Swift - 使用字典 - 添加多个值 [英] Swift - Working with Dictionaries - Add multiple values

查看:362
本文介绍了Swift - 使用字典 - 添加多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找这个问题的答案,但不幸的是没有成功。



我正在开发一个数学应用程序



(我需要使用解析器在NSView上绘制函数)



数据结构保存词典,但我无法



字典初始化 strong> like:

  var functions = [String:[[String:NSBezierPath],[String:NSColor],[String: CGFloat],[String:Bool]]](); 

//1A。主字典的字符串键是函数的值,例如sin(x)
//1B。Dictionary的值是一个`Array` od`字典`

//2.第一个值是一个字典,其关键是一个字符串和值NSBezierPath()
//3.第二个值是一个字典,其关键是一个字符串和值NSColor()
//4。第三个值是一个字典,其关键是一个字符串和值CGFloat()
//5第一个值是一个字典,其关键是字符串和值Bool()

要添加函数,方法(我将报告一部分):

  ... 

//构建子-dictionaries

let path:[String:NSBezierPath] = [path:thePath];
let color:[String:NSColor] = [color:theColor];
let line:[String:CGFloat] = [lineWidth:theLine];
let visible:[String:Bool] = [visible:theVisibility];

//注意,我100%确保相对值与相对类型兼容。
//因此我很肯定有一个语法错误。


//添加元素(注意:函数是一个字符串,我希望它是Dictionary的键)

functions [theFunction ] = [path,color,line,visible]; // Error here

...

错误

 '@ | value $ T10'不等于'(String,[ [String:NSbezierPath],[String:NSColor],[String:CGFloat],[String:Bool])])'


b $ b

我希望这个问题足够清楚和完整。



如果我会立即添加您需要的任何类型的信息。

$

解决方案 div>

词典从特定键类型映射到特定值类型。例如,您可以将您的键类型 String 和您的值类型 Int



在你的情况下,你已经声明了一个奇怪的字典:从 String s(足够好)映射到一个4元组4个不同的字典类型(每个字符串到不同类型)。



(这是一个新的字典类型,但它看起来像这样:

  var thingy = [String,String]()

$ b b

是这样的缩写:

  var thingy = [(String,String)] b $ b  

Huh。奇怪,但它的工作原理你的字典正在使用这个伎俩的变体)



这意味着你的赋值工作你需要创建一个4元组的数组(注意额外的括号):

  functions [theFunction] = [(path,color,line,visible)] 

我猜你不是要这样做。你真的想要这4种不同的字典类型的数组吗?如果是这样,你运气不好 - 你不能在同一个数组中存储不同的类型(字典有不同类型的值)。



(嗯,如果你创建字典的值 Any - 但这是一个可怕的想法,是噩梦使用)



可能你想要的结果是这样的(即从字符串中创建函数字典映射不同类型的字典的4元组):

  var functions = [String:([String:NSBezierPath] String:NSColor],[String:CGFloat],[String:Bool])]()



'd给这样的字典赋值(注意,rhs上没有方括号):

  functions [theFunction] =路径,颜色,线,可见)

这将工作,但是会很不愉快。但你真的想把结构化数据存储在字典和数组中吗?这不是JavaScript ;-)你会绑定在多层次字典中的节点。声明结构体!这将是更容易在你的代码中使用。

  struct函数{
var beziers:[String:NSBezierPath]
var color:[String:NSColor ]
var line:[String:NSColor]
var float:[String:CGFloat]
var bools:[String:Bool]
}
var functions: String:Functions] = [:]

更好的是,如果所有的bezier,是具有相同键的引用,声明包含所有它们或类似的字典。


I've been looking for answers to this problem, but unfortunately without success.

I'm developing a mathematical app (Swift-based), which keeps data of every function the user enters.

(I then need to draw the functions on an NSView using a Parser)

The data structure is saved into a Dictionary but I'm not able to add values and keys.

The Dictionary is initialized like:

var functions = [String : [[String : NSBezierPath], [String : NSColor], [String : CGFloat], [String : Bool]]]();

//1A.The String key of the main Dictionary is the value of the function, such as "sin(x)"
//1B.The value of the `Dictionary` is an `Array` od `Dictionaries`

//2.The first value is a dictionary, whose key is a String and value NSBezierPath()
//3.The second value is a dictionary, whose key is a String and value NSColor()
//4.The third value is a dictionary, whose key is a String and value CGFloat()
//5.The first value is a dictionary, whose key is a String and value Bool()

To add the functions, I have implemented a method (I will report a part of) :

...

//Build the sub-dictionaries

let path : [String:NSBezierPath] = ["path" : thePath];
let color : [String:NSColor] = ["color" : theColor];
let line : [String:CGFloat] = ["lineWidth" : theLine];
let visible : [String:Bool] = ["visible" : theVisibility];

//Note that I'm 100% sure that the relative values are compatible with the relative types.
//Therefore I'm pretty sure there is a syntax error.


//Add the element (note: theFunction is a string, and I want it to be the key of the `Dictionary`)

functions[theFunction] = [path, color, line, visible]; //Error here

...

I'm given the following error:

'@|value $T10' is not identical to '(String,[([String:NSbezierPath],[String : NSColor],[String : CGFloat],[String : Bool])])'

I hope the question was enough clear and complete.

In case I will immediately add any kind of information you will need.

Best regards and happy holidays.

解决方案

Dictionaries map from a specific key type to a specific value type. For example, you could make your key type String and your value type Int.

In your case, you’ve declared quite a strange dictionary: a mapping from Strings (fair enough), to an array of 4-tuples of 4 different dictionary types (each one from strings to a different type).

(It’s a new one on me, but it looks like this:

var thingy = [String,String]() 

is shorthand for this:

 var thingy = [(String,String)]()  

Huh. Strange but it works. Your dictionary is using a variant of this trick)

This means to make your assignment work you need to create an array of a 4-tuple (note additional brackets):

functions[theFunction] = [(path, color, line, visible)]

I’m guessing you didn’t mean to do this though. Did you actually want an array of these 4 different dictionary types? If so, you’re out of luck – you can’t store different types (dictionaries that have different types for their values) in the same array.

(Well, you could if you made the values of the dictionary Any – but that’s a terrible idea and would be nightmare to use)

Probably the result you wanted was this (i.e. make the functions dictionary map from a string to a 4-tuple of dictionaries of different types):

var functions = [String : ([String : NSBezierPath], [String : NSColor], [String : CGFloat], [String : Bool])]()

You’d assign values to the dictionary like this (note, no square brackets on the rhs):

functions[theFunction] = (path, color, line, visible)

This will work but it will be pretty unpleasant to work with. But do you really want to store your structured data in dictionaries and arrays? This isn’t JavaScript ;-) You’ll tie yourself in knots navigating that multi-level dictionary. Declare a struct! It’ll be so much easier to work with in your code.

struct Functions {
    var beziers: [String:NSBezierPath]
    var color: [String:NSColor]
    var line: [String:NSColor]
    var floats: [String:CGFloat]
    var bools: [String:Bool]
}
var functions: [String:Functions] = [:]

Even better, if all the beziers, colors etc are supposed to be references with the same key, declare a dictionary that contains all of them or similar.

这篇关于Swift - 使用字典 - 添加多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆