我正在尝试从通知中获取 objectForKey,但出现错误:anyobject does not have a membered named objectforkey
我使用的代码是:
func executeAlbum(notification:NSNotification){
let data = notification.userInfo.objectForKey("data") as [AlbumModel] // ERROR HERE
self.sources = data
self.albumTable?.reloadData()
}
谁能告诉我为什么它不起作用以及等效物是什么?
谢谢
请您参考如下方法:
objectForKey 方法是 NSDictionary 的一种方法,在您的情况下 notification.userInfo 返回 [NSObject : AnyObject]? 哪种类型是 Dictionary。要从“swift 字典”中获取对象,您需要使用其下标方法,因此:
func executeAlbum(notification:NSNotification){
let data:[AlbumModel]? = notification.userInfo?["data"] as? [AlbumModel] // ERROR HERE -> Not anymore
self.sources = data
self.albumTable?.reloadData()
}
应该做的工作
