我正在尝试使用Swift等效initWithContentsOfFile:的Array.文档说明等效的是convenience init(contentsOfFile aPath: String!)
我试图用以下代码调用它:
var path = NSBundle.mainBundle().pathForResource("Standards", ofType: "plist")
var rawStandards = Array<Dictionary<String, AnyObject?>>(contentsOfFile: path)
Run Code Online (Sandbox Code Playgroud)
但是,编译器告诉我,它无法找到接受所提供参数的重载.我错过了什么?
@Leo Natan 非常接近,但对我有用的方式是:
var rawStandards = NSArray(contentsOfFile: path)
Run Code Online (Sandbox Code Playgroud)
然后我能够访问这些元素:
for obj: AnyObject in rawStandards {
if var rawStandard = obj as? NSDictionary {
}
}
Run Code Online (Sandbox Code Playgroud)
您正在初始化一个 Swift 数组,而不是NSArray具有initWithContentsOfFile:初始化方法的 。
尝试:
var rawStandards = NSArray(contentsOfFile: path) as Dictionary<String, AnyObject?>[]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4761 次 |
| 最近记录: |