Convert CGFloat to NSNumber

2015-02-24 Thread Charles Jenkins
I’m surprised how painful it is to do trivial things in Swift. 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?

— 

Charles
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Convert CGFloat to NSNumber

2015-02-24 Thread Roland King

 On 24 Feb 2015, at 18:57, Charles Jenkins cejw...@gmail.com 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 (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Convert CGFloat to NSNumber

2015-02-24 Thread Charles Jenkins
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 (r...@rols.org) wrote:


On 24 Feb 2015, at 18:57, Charles Jenkins cejw...@gmail.com 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 (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Convert CGFloat to NSNumber

2015-02-24 Thread Roland King

 On 25 Feb 2015, at 00:14, Charles Jenkins cejw...@gmail.com 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. Thanks!
 
 Swift is a language I want to like, but currently it makes the easy stuff 
 hard without making the hard stuff any easier. 
 
 — 


Well it’s a swift structure, which doesn’t really mean much more than it’s an 
object with copy semantics. It has a TypeAlias for double which is where it 
stores the value. So it’s a structure, but not possibly in the way you would 
naturally infer. I should have been clearer. 

Anyway if you type

var xx : CGFloat

 and then Cmd-Right-Click on the CGFloat you’ll see what it is and where I got 
the nativevalue thing from. 


 
 Charles
 
 On February 24, 2015 at 7:45:22 AM, Roland King (r...@rols.org 
 mailto:r...@rols.org) wrote:
 
 
 On 24 Feb 2015, at 18:57, Charles Jenkins cejw...@gmail.com 
 mailto:cejw...@gmail.com 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 (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Convert CGFloat to NSNumber

2015-02-24 Thread Marco S Hyman

 On Feb 24, 2015, at 10:16 AM, Quincey Morris 
 quinceymor...@rivergatesoftware.com wrote:
 
 The following work, too (Xcode 6.1.1):
 
   let f1: NSNumber = font.pointSize
   let f2 = font.pointSize as NSNumber
 
 ...
 
 (Things may have changed in Swift 1.2, though.)
 


Works fine in 1.2, too.

$ swift
Welcome to Swift version 1.2. Type :help for assistance.
  1 import Foundation
  2 let f0: CGFloat = 42.0
f0: CGFloat = 42
  3 let f1: NSNumber = f0 
f1: NSNumber = Double(42)
  4 let f2 = f0 as NSNumber
f2: NSNumber = Double(42)


Marc


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Convert CGFloat to NSNumber

2015-02-24 Thread Kyle Sluder
On Tue, Feb 24, 2015, at 04:57 AM, Charles Jenkins wrote:

 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 )

You need to create either a Double or Float from the CGFloat before you
can pass it to the NSNumber initializer:

  1 let f: CGFloat = 2.0
f: CGFloat = 2
  2 let p = NSNumber(double: Double(f)) 
p: NSNumber = Double(2)

--Kyle

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Convert CGFloat to NSNumber

2015-02-24 Thread Quincey Morris
On Feb 24, 2015, at 08:14 , Charles Jenkins cejw...@gmail.com 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 k...@ksluder.com 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 (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com