Henry Brown
Henry Brown

Reputation: 2237

anyobject does not have a membered named objectforkey in SWIFT

I am trying to get the objectForKey from a notifaction, although I am getting the error: anyobject does not have a membered named objectforkey

The code I am using is:

func executeAlbum(notification:NSNotification){
        let data = notification.userInfo.objectForKey("data") as [AlbumModel] // ERROR HERE
        self.sources = data
        self.albumTable?.reloadData()
    }

Can anybody tell me why it won't work and what the equivalent is?

Thanks

Upvotes: 1

Views: 2052

Answers (2)

Zell B.
Zell B.

Reputation: 10286

objectForKey method is a method of NSDictionary and in your case notification.userInfo return [NSObject : AnyObject]? which type is Dictionary. To get object from "swift dictionary" you will need to use its subscript method, so:

func executeAlbum(notification:NSNotification){
        let data:[AlbumModel]? = notification.userInfo?["data"] as? [AlbumModel] // ERROR HERE -> Not anymore
        self.sources = data
        self.albumTable?.reloadData()
    }

should do the work

Upvotes: 3

SparkierFlunky
SparkierFlunky

Reputation: 494

Your notification.userInfo is of type AnyObject. And therefore you can't use objectForKey("") because AnyObject does not provide this method. You might need to cast notification.userInfo but i don't know what is behind that.

Upvotes: 0

Related Questions