Re: Convert CGFloat to NSNumber
Re: Convert CGFloat to NSNumber
- Subject: Re: Convert CGFloat to NSNumber
- From: Charles Jenkins <email@hidden>
- Date: Tue, 24 Feb 2015 11:14:40 -0500
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. Thanks!
Swift is a language I want to like, but currently it makes the easy stuff hard without making the hard stuff any easier.
—
Charles
On February 24, 2015 at 7:45:22 AM, Roland King (email@hidden) wrote:
On 24 Feb 2015, at 18:57, Charles Jenkins <email@hidden> wrote:
I’m surprised how painful it is to do trivial things in Swift.
I’ve stopped being surprised at this.
Between the anal type checking and the spew of optionals I spend all my time fiddling around trying to get a ‘?’ in the right place or splitting lines up
into
individual
expressions
so that I
can check the
type
of
each line
I’m hoping the improved error checking in the latest Swift 1.2 beta is going to help with this, but that version is currently buggy enough it crashes on my example code so I’m waiting for some of those bugs to get fixed before I try Swift again in earnest.
All I want to do is convert NSFont.pointSize to an NSNumber, but I can’t figure out any syntax the Swift compiler will accept.
My latest fruitless attempt has involved trying to simply cast the value into something for which NSNumber has a corresponding init():
let size:Float = font.pointSize as Float
let points = NSNumber( float: size )
Neither Float nor Double works. What the heck is a Swift CGFloat that seemingly makes it incompatible with everything else?
It’s a structure. Cmd-RightClick is your friend here.
I ended up with this piece of slightly non-obvious code, there’s probably three other ways to do it.
import Cocoa
let font = NSFont(name: "Helvetica", size: 29 );
let rs = NSNumber( double: font!.pointSize.native )
An example of the ‘fiddling about’ I was talking about, before I got to those lines, I thought I’d just check I had made the font I wanted by constructing an NSAttributedString with it, I had this
let str = NSAttributedString(string: "test string", attributes: [ NSFontAttributeName : font ] )
which gives an error message that there isn’t an initializer which accepts string: String, attributes : [ String, NSFont? ]. I split the line up to construct the attributes separately and defined it to be [ NSObject : AnyObject ] (which is what that initializer takes) and eventually stumbled on the realization I had to unwrap font in order for it to work. I spent a month nearly doing nothing but Swift and I never really got much better at it. Perhaps I’m too ancient and my brain is wired up wrong from years of C but I don’t find Swift an easy language to use at all and spend lots of unproductive time trying to sort out silly things like the above.
_______________________________________________
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