我知道如何在swift中创建一个数组,只需
var arr = ["Foo", "Bar"]
但我不确定如何将其转换为swift:
NSArray *coachMarks = @[
@{
@"rect": [NSValue valueWithCGRect:(CGRect){{0,0},{45,45}}],
@"caption": @"Helpful navigation menu"
}
];
有什么想法吗?
答案 0 :(得分:3)
你想在数组中存储字典,这是一步一步的过程如何实现:
let rect = CGRect(x: 0, y: 0, width: 45, height: 45)
let dict1 = ["rect": NSValue(CGRect: rect)]
let dict2 = ["caption": "Helpful navigation menu"]
let array: [AnyObject] = [dict1, dict2]
编辑: 对不起,我在数组中插入了两个词典,但在你的问题中你只有一个,这是更正的答案:
let rect = CGRect(x: 0, y: 0, width: 45, height: 45)
let dict1 = ["rect": NSValue(CGRect: rect), "caption": "Helpful navigation menu"]
let array: [AnyObject] = [dict1]
答案 1 :(得分:1)
它看起来像这样
var coachMarks: Array<Dictionary<String, AnyObject>> = [["rect": NSValue(CGRect: CGRectMake(0.0, 0.0, 45.0, 45.0)),
"caption": "Helpful navigation menu"]]
答案 2 :(得分:1)
尝试这样的事情,
var coachMarks : [AnyObject] = [ ["rect" : NSValue(rect: NSRect(origin: CGPointMake(0, 0), size: CGSizeMake(45, 45))), "caption" : "Helpful navigation menu"]]