I am facing with the following error during compilation based on 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'
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. 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).
I just changed it to this. Then it fixed.
label.text = (detail.value(forKey: "timeStamp") as! NSObject).description
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.