[swift-users] ?? operator question

Marco Feltmann coding at marcofeltmann.de
Tue Apr 19 06:09:38 CDT 2016


Just guessed since I try to separate Swift from Objective-C frameworks.

Am 19.04.2016 um 03:15 schrieb zh ao:
> I don't think NSNumber is OK here. As t1 is a struct not a literal value.
In fact t1 in an OPTIONAL struct.
That means it can point to anything from `nil` to Int Struct, NSInteger 
and NSNumber.
AFAIR it automatically bridges to NSNumber as soon as you import 
Foundation or a similiar framework. Same works with String. String is a 
struct used in plain Swift and NSString used with Foundation.
> If NSNumber is OK. I think the compiler is too smart than it should be.
Indeed it is. Way to smart.
It is that smart that it sometimes stands in its own way.
At least when used with Objective-C. Your question outlines one of those 
strange side effects.
> Also, you should aware that right side of let y works in print(), but does
> not work in let, which is not consistent somehow.
It is consistent.
You can map an OPTIONAL integer to NSNumber (using Foundation framework) 
but you cannot map an OPTIONAL integer to NSString.

"But wait a sec, how can NSFount(name: "", size: 0) result in an 
NSNumber?" you may ask.
I don't now.
The behaviour of NSFonts +fonttWithName:size: for empty/not found font 
name is not defined in the documentation, so ANY result is correct.


More information about the swift-users mailing list