This morning, I discovered that Swift uses a different cross platform approach to Objective C. To determine the OS for type aliasing, use os() and pass it either OSX or iOS, as in the following example:
// OSX, iOS #if os(iOS) typealias View = UIView #else typealias View = NSView #endif
Once defined, the following code ran on both iOS and OS X. It compiled without incident and correctly reported the correct class of var myView = View() when I ran println("\(myView.description)") In addition to OS testing, you can check whether your code is running on the simulator or on-device. The following code all seemed to behave properly in my tests.
// x86_64, arm, arm64, i386 #if os(iOS) println("iOS") #if arch(i386) || arch(x86_64) println("Simulator") #else #if arch(arm) println("Device (arm)") #else println("Device (arm64)") #endif #endif #else println("OS X") #endif
I should mention that compiling to device had several issues. First, Xcode gave me a bit of a hard time about code signing, which I bypassed by linking the codesign_allocate from Xcode 5 (/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/codesign_allocate) to /usr/bin. I also had to hand-update the developer settings for development/distribution.
Second, I could not get anything to run with my iOS 8 device. This is apparently a known problem. I ended up restoring the thing, and it still doesn’t work. So I just put it aside for now and am compiling for the moment to my primary iOS 7 device, which is fine for early tests, but not great for new APIs. I’ll update if I get the #$@%ing device working. (And no, rebooting everything multiple times didn’t work, nor did disconnect/reconnect. It’s really chicken-entrails-time here.)
Final pro tip. In the Devices organizer in new Xcode, right-click the device and choose Show Provisioning Profiles to access them. Here is also where you can click to rename your device, and because it’s a standard text field, it will even offer to spell-correct your device name with hilarious suggestions.
Comments are closed.