【发布时间】:2019-02-19 11:06:58
【问题描述】:
我来这里是为了了解为什么我实施的解决方案不起作用。基本上我有一个名为MyClass 的类,在这个类中我想要一个从plist 文件创建的静态字典。像这样:
class MyClass {
static var myDic: [String: String] = NSDictionary(contentsOfFile: Bundle(for: self).path(forResource: "filename", ofType: "plist")!) as! [String: String]
}
如果我这样做,编译器会抱怨:
Cannot convert value of type '(MyClass) -> () -> (MyClass)' to expected argument type 'AnyClass' (aka 'AnyObject.Type')
但如果我更改 myDic var 并创建一个返回该 dic 的静态方法,一切都很好:
class MyClass {
static func myDic() -> [String: String] {
return NSDictionary(contentsOfFile: Bundle(for: self).path(forResource: "PlayerRolesWithColors", ofType: "plist")!) as! [String: String]
}
}
这里有两个问题:
- 编译器错误中的这个 sintax 是什么意思?
'(MyClass) -> () -> (MyClass)' - 这两种情况有什么区别?为什么前者不起作用而后者很好?
谢谢。
【问题讨论】:
-
好问题,值得回答:)