我正在尝试在Array上使用initWithContentsOfFile:的Swift等价物。文档中指出了等效的是convenience init(contentsOfFile aPath: String!)
我试着用下面的代码来调用它:
var path = NSBundle.mainBundle().pathForResource("Standards", ofType: "plist")
var rawStandards = Array<Dictionary<String, AnyObject?>>(contentsOfFile: path)然而,编译器告诉我,它找不到接受所提供参数的重载。我遗漏了什么?
发布于 2014-06-11 02:00:33
@Leo Natan非常接近,但对我有效的方式是:
var rawStandards = NSArray(contentsOfFile: path)然后我就可以访问元素了:
for obj: AnyObject in rawStandards {
if var rawStandard = obj as? NSDictionary {
}
}发布于 2014-06-11 00:26:44
您正在初始化Swift数组,而不是具有initWithContentsOfFile:初始值设定项方法的NSArray。
尝试:
var rawStandards = NSArray(contentsOfFile: path) as Dictionary<String, AnyObject?>[]发布于 2014-06-11 01:35:35
var path = NSBundle.mainBundle().pathForResource("Standards", ofType: "plist")
var menuItemArray = NSArray(contentsOfFile: path) as NSArray!这就是我自己使用的。在viewDidLoad中
https://stackoverflow.com/questions/24145932
复制相似问题