using CFNumberRef safely in swift?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

using CFNumberRef safely in swift?

Perry E. Metzger
I'm using the power source notification API on OS X and it is handing
me dictionaries containing CFNumberRef objects. How can I safely read
those inside swift and turn them into integers (or whatever they're
supposed to be)?

Perry
--
Perry E. Metzger [hidden email]

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/20151130092147.7156b6f0%40jabberwock.cb.piermont.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: using CFNumberRef safely in swift?

Brent Royal-Gordon
CFNumberRef is toll-free bridged to NSNumber, so you can upcast to NSNumber and then call NSNumber APIs on them.

Sent from my iPad

> On Nov 30, 2015, at 6:21 AM, Perry E. Metzger <[hidden email]> wrote:
>
> I'm using the power source notification API on OS X and it is handing
> me dictionaries containing CFNumberRef objects. How can I safely read
> those inside swift and turn them into integers (or whatever they're
> supposed to be)?
>
> Perry
> --
> Perry E. Metzger        [hidden email]
>
> --
> You received this message because you are subscribed to the Google Groups "Swift Language" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
> To post to this group, send email to [hidden email].
> To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/20151130092147.7156b6f0%40jabberwock.cb.piermont.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/56B327EB-6F69-4960-813D-5441821455C0%40architechies.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: using CFNumberRef safely in swift?

Perry E. Metzger
On Mon, 30 Nov 2015 19:54:17 -0800 Brent Royal-Gordon
<[hidden email]> wrote:
> CFNumberRef is toll-free bridged to NSNumber, so you can upcast to
> NSNumber and then call NSNumber APIs on them.

I don't suppose you could provide a code sample? I'm afraid I'm too
much of a beginner to understand exactly what this implies.

Perry
--
Perry E. Metzger [hidden email]

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/20151201070227.1a07362e%40jabberwock.cb.piermont.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: using CFNumberRef safely in swift?

Brent Royal-Gordon
> I don't suppose you could provide a code sample? I'm afraid I'm too
> much of a beginner to understand exactly what this implies.

Sure. (I would’ve done that in the first place if I’d had my Mac handy.) Just paste the following into a new playground:

//: Number Bridging Playground

import Foundation

//: So, let's make a `CFNumber` to work with.
let myCFNumber: CFNumber = 3.14159

//: `CFNumber` and `NSNumber` are toll-free bridged—at the Objective-C level, they are completely
//: interchangeable to the point that you can just cast the bits of one to the other.
//: This also means that in Swift, you can cast a `CFNumber` directly to an `NSNumber`.
//: This is an "upcast"—it can't fail—so you can just use `as`, not `as?` or `as!`.
let myNSNumber = myCFNumber as NSNumber

//: Now that it's an `NSNumber`, you can use `NSNumber`'s methods and properties.
myNSNumber.integerValue
myNSNumber.doubleValue

//: Or you can cast to a numeric type, which Swift allows as part of its Objective-C bridging.
//: (This is not quite the same as toll-free bridging—`NSNumber` and `Int` aren’t really interchangeable,
//: but as a convenience, Swift converts automatically when you cast between them.)
//: Again, these are upcasts so you can just use `as`.
myNSNumber as Int
myNSNumber as Double

//: In fact, you can even do that to the `CFNumber` directly, without first casting to `NSNumber`.
myCFNumber as Int
myCFNumber as Double

//: So, in conclusion, `CFNumber` and `NSNumber` are exactly equivalent to one another. Either one
//: can be cast to a Swift numeric type like `Int` or `Double` and Swift will convert them for you.
//:
//: Hope this helps!

--
Brent Royal-Gordon
Architechies

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/8DCA6DCF-C9F1-479C-A479-70CC3BE3786E%40architechies.com.
For more options, visit https://groups.google.com/d/optout.