iOSAppGuy
iOSAppGuy

Reputation: 633

When using NSNotificationCenter, why must my userInfo be of type [NSObject : AnyObject]?

I am simply trying to pass my WorkoutObject in an NSNotification using Swift 2.0. Here is my code:

let myDict = ["MyWorkout" : myWorkoutObj]
NSNotificationCenter.defaultCenter().postNotificationName("WorkoutNotification", object: self, userInfo: myDict)

The complier doesn't like myDict because it is in the form of [String:WorkoutObj] instead of [NSObject:AnyObject]. I've tried countless ways of fixing this:

I tried casting my WorkoutObj to AnyObject. That doesn't solve the problem.

I tried putting ? and or ! around my WorkoutObj. That doesn't solve the problem.

Why can't I pass the object in a form of [String:WorkoutObj]? And what must I do to make this happen?

Upvotes: 0

Views: 38

Answers (1)

iOSAppGuy
iOSAppGuy

Reputation: 633

I figured it out.

My WorkoutObject was a struct instead of a class. Once I changed it to a class everything worked.

Upvotes: 2

Related Questions