I’m trying to send an int (which is defined in an enum) as the object in an NSNotification.
I can’t do this, because I’ll get an EXC_BAD_ACCESS error thrown when trying to access the object in the notify: method of the observer, since int is not of type id (I think?).
My current solution is to cast the int to an NSNumber (by using numberWithInt:). While this works it doesn’t feel right or clean.
So the question is: what is the correct way to pass an int as the “object” of an NSNotification?
I guess this actually has nothing to do with this specific case, but a Cocoa thing in general 🙂
You’re not casting to
NSNumber… you’re creating aNSNumberobject and setting the contents (or value) of it to yourint.And Objective C objects are what you can pass in the
userInfodictionaries of NSNotifications, so what you are doing is absolutely correct.Now as to the crash, please edit your question to show code as to how you are creating and posting your
NSNotificationand what the method is that actually handles the receivedNSNotificationyou sent.