Due to NSNotificationCenter.defaultCenter().postNotificationName userinfo only accepting dictionaries with data complying with AnyObject protocol, does anyone have any suggestions how to post structs as part of an NSNotification?
My initial thought it is to wrap the struct in a class - but then what would be the point in using a struct in the first place.
Am I missing something or is this just a result of conflating Swift with API's built for Objective C?
Here's a demonstration of what I'm describing: -
class wrapper: NSObject {
var aStructToWrap: aStruct
init(theStruct: aStruct) {
aStructToWrap = theStruct
super.init()
}
}
struct aStruct {
var aValue: String
}
let aRealStruct = aStruct(aValue: "egg")
NSNotificationCenter.defaultCenter().postNotificationName("aKey", object: nil, userInfo: ["anotherKey": aRealStruct]) // ERR: Extra argument 'userinfo' in call
let wrappedStruct = wrapper(theStruct: aRealStruct)
NSNotificationCenter.defaultCenter().postNotificationName("aKey", object: nil, userInfo: ["anotherKey": wrappedStruct]) // no error