在基于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.
}
}
错误:
/.../DetailViewController.swift:27:33:'AnyObject'不是以下内容的子类型
'NSObject'
最佳答案
if let detail: [String: AnyObject] = self.detailItem {
if let label = self.detailDescriptionLabel {
label.text = detail.valueForKey("timeStamp")!.description
}
}
如果
detailItem
用于存储JSON对象,则最好在一开始将其转换为字典。好像变量被强制转换为AnyObject
一样,编译器将继续提示错误,因为它无法处理它(对于您而言,AnyObject
类没有valueForKey
函数)。
关于ios - swift3中的编译错误:“AnyObject”不是“NSObject”的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41298183/