using CFNumberRef safely in swift?

364 views
Skip to first unread message

Perry E. Metzger

unread,
Nov 30, 2015, 9:21:50 AM11/30/15
to swift-l...@googlegroups.com
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 pe...@piermont.com

Brent Royal-Gordon

unread,
Nov 30, 2015, 10:54:21 PM11/30/15
to Perry E. Metzger, swift-l...@googlegroups.com
CFNumberRef is toll-free bridged to NSNumber, so you can upcast to NSNumber and then call NSNumber APIs on them.

Sent from my iPad
> --
> 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 swift-languag...@googlegroups.com.
> To post to this group, send email to swift-l...@googlegroups.com.
> 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.

Perry E. Metzger

unread,
Dec 1, 2015, 7:02:31 AM12/1/15
to Brent Royal-Gordon, swift-l...@googlegroups.com
On Mon, 30 Nov 2015 19:54:17 -0800 Brent Royal-Gordon
<br...@architechies.com> 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.

Brent Royal-Gordon

unread,
Dec 1, 2015, 7:27:31 AM12/1/15
to Perry E. Metzger, swift-l...@googlegroups.com
> 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

Reply all
Reply to author
Forward
0 new messages