Search code examples
swiftnsdictionarynsnotificationcenter

Dictionary value type in NSNotificationCenter userInfo


When implementing the next code:

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {

    NSNotificationCenter.defaultCenter().postNotificationName("gotAuthorization", object: nil, userInfo: ["status": status])

}

I'm getting the next error:

cannot invoke 'postNotificationName' with argument list of type...

On the other hand, if I change the status to some simple value like a string or integer, everything is ok.

CLAuthorizationStatus is an enum and it is Int32 eventually. So what's the problem putting it as a value to Dictionary?

What am I missing here :\


Solution

  • The argument takes a dictionary of [NSObject:AnyObject]? and don't mean to sound like the compiler but Int32 do not conform to AnyObject protocol. Its not automatically bridged to the NSNumber so you need to cast it.

    ["status": Int(status.rawValue)]