Compile Swift on 10.9

Compile Swift on 10.9?

Ok, so here's the scoop:

After downloading the GM you will have a non-functional Xcode, Apple forgot to include the sdk in the app bundle. So now you have to download the 6.1 beta as well, Show Package Contents, and go to the folder:

/Volumes/Xcode/Xcode-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer

Note I have opened the app bundle from the disk image, thus the volume name. Inside that SDKs folder you will find MacOSX10.9.sdk and MacOSX10.10.sdk. The GM release is missing the later. So make a copy of that somewhere handy, like your desktop.

OK, now find the same path in the GM version. Mine's installed in my Applications folder, so it's here:

/Applications/Xcode 6.app/Contents/Developer/Platforms/MacOSX.platform/Develope

You will note that the MacOSX10.10.sdk folder is missing. Nice. Ok, copy the version from the beta into this folder.

Restart Xcode, open your project and Clean. Now look VERY CAREFULLY for every setting for SDK or deployment target. The SDK has to be 10.10, the deployment target has to be 10.9. This appears in many places on several screens, so look carefully.

Compiles and runs. And fixes the crashes I was seeing in Xcode in B6 and B7.

Do Swift-based applications work on OS X 10.9/iOS 7 and lower?

I just tested it for you, Swift applications compile into standard binaries and can be run on OS X 10.9 and iOS 7.


Simple Swift application used for testing:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

var controller = UIViewController()
var view = UIView(frame: CGRectMake(0, 0, 320, 568))
view.backgroundColor = UIColor.redColor()
controller.view = view

var label = UILabel(frame: CGRectMake(0, 0, 200, 21))
label.center = CGPointMake(160, 284)
label.textAlignment = NSTextAlignment.Center
label.text = "I'am a test label"
controller.view.addSubview(label)

self.window!.rootViewController = controller
self.window!.makeKeyAndVisible()
return true
}

Will Swift 2.0 be compatible with OS X 10.9 or lower?

Swift's oldest supported deployment target is still OS X 10.9 and iOS 7. This is unchanged in Swift 2.

Certain APIs, however, are only available on later versions of these operating systems. I suspect this is what you mean by:

documentation that says Availability OS X 10.10 or later - in simple NS class docs

(As Martin R. notes in the comments, this is unrelated to the language, except that "Swift has better runtime check mechanisms.")

With Swift 2, you can check API availability using the #available syntax. Here's an example from the Xcode release notes:

if #available(iOS 8.0, OSX 10.10, *) {
// Use Handoff APIs when available.
let activity = NSUserActivity(activityType:"com.example.ShoppingList.view")
activity.becomeCurrent()
} else {
// Fall back when Handoff APIs not available.
}

How to mark Swift declaration as available in iOS and unavailable in macOS

Wrap it inside this

#if canImport(UIKit)
// iOS Only code
#endif

This way it will remove from compiled project if it's not going to iOS device.

You can check with OS type like #if os(iOS) too, but be specific to why you should care about the OS? isn't it because of something especially for iOS like UIKit ? If not, be more general and limit the OS as @Shreeram mentioned.

Is it possible to code in Swift and deploy to real life devices on Windows?

Right now Swift is only for MacOS, iOS and linux devices available. While there are unofficial ports for windows like this one: Swift for Windows and Robert mentioned. The best solution right now to have the latest version of swift and run it is to buy either a Mac or an iOS device or an ubuntu device.

Why does XCode suggest (and compile) non-existant object properties?

On every iOS and MacOS X version, there are three important version numbers: The version number of the SDK that you are using (what you use in your compiler), the version number of the lowest iOS or MacOS X version that allows your app to run, and the version number of the actual device your code is running on.

The SDK number tells the compiler which OS methods you can call. If your SDK is MacOS X 10.9 then you cannot call methods from MacOS X 10.10, the compiler doesn't allow it.

If your deployment version (the lowest version that allows the app to run) is for example 10.9 and the SDK is 10.10 then methods that only exist will crash if you try to call them when running on 10.9. You need to check this manually; Swift 2 has methods that make this very easy.

In this situation, the app will not run on 10.8 at all. It will run on 10.9 but crash if you use 10.10 methods. It will run fine on 10.9.

The idea behind the setup is that it lets you use new features running on a new OS version, while being able to run on an old OS version, assuming you are careful to avoid new calls.

If that's too complicated for you, either use the 10.9 SDK (no 10.10 calls for you, ever), or deploy on 10.10 only (your app won't run on 10.9).



Related Topics



Leave a reply



Submit