我一直在寻找这个问题的答案,但不幸的是没有成功。Swift - 使用字典 - 添加多个值
我正在开发一个数学应用程序(基于Swift),它保存用户输入的每个函数的数据。
(我这时就需要使用分析器画上的NSView功能)
数据结构保存成Dictionary
但我不能增加值和键 。
的Dictionary
是初始化,如:
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()
要添加的功能,我实现了一个方法(我将报告的一部分):
...
//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
...
我给出的以下错误:
'@|value $T10' is not identical to '(String,[([String:NSbezierPath],[String : NSColor],[String : CGFloat],[String : Bool])])'
我希望这个问题足够清晰和完整。
万一我会立即添加任何您需要的信息。
此致敬意和节日快乐。
谢谢你的回答。 它帮了我很多! 我终于决定按照你的建议来声明一个结构体,它的功能就像一个魅力!再次感谢你! –