NSNotificationCenter passing structs as part of the UserInfo NSNotificationCenter passing structs as part of the UserInfo swift swift

NSNotificationCenter passing structs as part of the UserInfo


The issue is that the original Obj-C method requires an NSDictionary, which only takes object types as keys and values, which translates to [AnyObject: AnyObject] in Swift, except NSDictionary likes to compare its keys with isEqual: which is in the NSObject protocol, so the key must be an NSObject (I don't know if NSObjectProtocol was sufficient but Apple has decided to make it an NSObject).Therefore, the NSDictionary userInfo must be [NSObject: AnyObject] in Swift, and so you can't put a struct in there, and I don't believe you could in Objective-C either.

Sadly a wrapper will be necessary. We could play with NSValue and produce something ugly and inefficient, but in any case the best solution is the wrapper you have created.

However, you made a subclass of NSObject, that wasn't needed, so you can throw that code away :)

class Wrapper {    var aStructToWrap: aStruct    init(theStruct: aStruct) {        aStructToWrap = theStruct    }}struct aStruct {    var aValue: String}

Except we can do even better! We can make a generic wrapper for any struct or value (or even object) you like.

class Wrapper<T> {    var wrappedValue: T    init(theValue: T) {        wrappedValue = theValue    }}struct aStruct {    var aValue: String}let aRealStruct = aStruct(aValue: "egg")let wrappedStruct = Wrapper(theValue: aRealStruct)NSNotificationCenter.defaultCenter().postNotificationName("aKey", object: nil, userInfo: ["anotherKey": wrappedStruct]) // no error

That's a mutable wrapper, feel free to make it immutable by switching the var for a let.