我正在將應用程序轉換爲swift3並遇到以下問題。[NSObject:AnyObject]類型的字典沒有成員「value(forKeyPath:...)」
@objc required init(response: HTTPURLResponse, representation: [NSObject : AnyObject])
{
if (representation.value(forKeyPath: "title") is String) {
self.title = **representation.value**(forKeyPath: "title") as! String
}
我收到以下錯誤:
Value of type [NSObject:AnyObject] has no member value.
在舊版本我只是用AnyObject類型爲代表的代碼,但如果我這樣做,我得到的錯誤AnyObject is not a subtype of NSObject
有:
if (representation.value(forKeyPath: "foo") is String) {
let elementObj = Element(response: response, representation:**representation.value(forKeyPath: "foo")**!)
}
'NSObject'和'(forKeyPath:「title」)'不會很好。是否可以使用'[String:AnyObject]'? –
爲什麼不簡單'self.title = representation [「title」]'? – vikingosegundo
你真的知道'value(forKeyPath:'應該做什麼的方法嗎? – vadian