There are a number of problems. Firstly, Int64 not an AnyObject . None of the primitive types of numbers are class. They can be tied to AnyObject using NSNumber , but you do not get automatic bridging for Int64 (see MartinR comment. I initially said that it was because it was wrapped in an option, but it was actually fixed -width )
Further, this syntax:
(something != nil) ? something! : nil
This is just a tricky way to say something .
The tool you want is map , so that you can take the optional and convert it to NSNumber if it exists.
dictionary["something"] = something.map(NSNumber.init)
Of course, if at all possible, get rid of AnyObject . This type is a huge pain and causes many problems. If it was [String: Int64] , you could simply:
dictionary["something"] = something
source share