[英]Compilation error in swift3: 'AnyObject' is not a subtype of 'NSObject'
I am facing with the following error during compilation based on Swift 3. 在基于Swift 3的编译过程中,我遇到以下错误。
class DetailViewController: UIViewController {
@IBOutlet weak var detailDescriptionLabel: UILabel!
var detailItem: AnyObject? {
didSet {
// Update the view.
self.configureView()
}
}
func configureView() {
// Update the user interface for the detail item.
if let detail: AnyObject = self.detailItem {
if let label = self.detailDescriptionLabel {
label.text = detail.valueForKey("timeStamp")!.description
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.configureView()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Error: 错误:
/.../DetailViewController.swift:27:33: 'AnyObject' is not a subtype of 'NSObject' /.../DetailViewController.swift:27:33:'AnyObject'不是'NSObject'的子类型
if let detail: [String: AnyObject] = self.detailItem {
if let label = self.detailDescriptionLabel {
label.text = detail.valueForKey("timeStamp")!.description
}
}
And if detailItem
is for storing JSON objects, you'd better cast it to dictionary at the beginning. 如果detailItem
用于存储JSON对象,则最好在一开始将其转换为字典。 As if the variable is cast as AnyObject
, the compile will keep prompting errors as it simply cant handle it (for your case, AnyObject
class does not have valueForKey
function). 就像变量被AnyObject
为AnyObject
,编译器将继续提示错误,因为它根本无法处理它(在您的情况下, AnyObject
类没有valueForKey
函数)。
I just changed it to this. 我只是将其更改为此。 Then it fixed. 然后修复。
label.text = (detail.value(forKey: "timeStamp") as! NSObject).description
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.