Re: Convert CGFloat to NSNumber
Re: Convert CGFloat to NSNumber
- Subject: Re: Convert CGFloat to NSNumber
- From: Quincey Morris <email@hidden>
- Date: Tue, 24 Feb 2015 18:16:23 +0000
On Feb 24, 2015, at 08:14 , Charles Jenkins <email@hidden> wrote:
>
> A structure?!? I did look it up in the documentation, and all I found was “the basic type for all floating-point values.” That the basis of all floating-point types could be a structure never occurred to me.
That’s not really what the quoted statement means.
On Feb 24, 2015, at 09:02 , Kyle Sluder <email@hidden> wrote:
> You need to create either a Double or Float from the CGFloat before you
> can pass it to the NSNumber initializer:
The following work, too (Xcode 6.1.1):
let f1: NSNumber = font.pointSize
let f2 = font.pointSize as NSNumber
What’s going on here, I think, is that NSNumber (in Obj-C) does not have an initializer that takes a CGFloat parameter, only initializers for float and double, but it never mattered (in Obj-C) because CGFloat was one of the other types. This carries over to Swift, except that there CGFloat is a distinct type, rather than being one or other of float or double.
AFAICT, the above forms work, not because there is an automatic conversion from CGFloat to double (let alone from CGFloat to NSNumber), but rather because CGFloat is FloatLiteralConvertible, and there are FloatLiteralConvertible variants of the ‘=‘ and ‘as’ operators.
(Things may have changed in Swift 1.2, though.)
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden