Question or problem with Swift language programming:
Will Swift-based applications work on OS X 10.9 (Mavericks)/iOS 7 and lower?
For example, I have a machine running OS X 10.8 (Mountain Lion), and I am wondering if an application I write in Swift will run on it.
Or what i should have to create a Swift application using Mac OS?
How to solve the problem:
Solution 1:
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 }
Solution 2:
Swift code can be deployed to OS X 10.9 and iOS 7.0. It will usually crash at launch on older OS versions.
Solution 3:
Apple has announced that Swift apps will be backward compatible with iOS 7 and OS X Mavericks. The WWDC app is written in Swift.
Solution 4:
Update – As per Xcode 6 Beta 4
iOS 7 and OS X 10.9 minimum deployment target
The Swift compiler and Xcode now enforce a minimum deployment target of iOS 7 or OS X
Mavericks. Setting an earlier deployment target results in a build failure.
From Xcode 6 release note
So my previous answer(Shown below) will not be applicable to any further development. Swift will no longer available for iOS6 and below
A Swift application can be run on iOS 6. Even though many people are saying that Swift will support only iOS 7+ and OS X 10.9+, from my experience it’s not.
I have tested a simple application written completely in Swift in an iOS 6 device. It works perfectly fine. As Apple says, Swift code is binary compatible with Objective-C code. It uses the same compiler and runtime to create the binary.
Here is the code I have tested:
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let button = UIButton.buttonWithType(UIButtonType.System) as UIButton button.frame = CGRectMake(100, 100, 100, 50) button.backgroundColor = UIColor.greenColor() button.setTitle("Test Button", forState: UIControlState.Normal) button.addTarget(self, action: "buttonTapped:", forControlEvents: UIControlEvents.TouchUpInside) self.view.addSubview(button) } func buttonTapped(sender: UIButton!) { println("buttonTapped") } }
It is a simple application, just adding a button programmatically. My application contains only two files, AppDelegate.swift
and ViewController.swift
.
So if you are not using any new APIs added as part of the iOS 8 SDK or some Swift specific APIs (corresponding API is not available for Objective-C) your application will seamlessly work on iOS 6 or later (tested and working), even on iOS 5 (not tested). Most of the APIs in Swift are just the replacement of the existing Objective-C APIs. In fact they are the same in binary.
Note: As per Xcode 6 beta 4 for swift apps deployment target should be iOS 7 or OS X 10.9(see the above update). So swift will no longer available for iOS6 and below
Solution 5:
In brief:
Swift based applications can target back to OS X Mavericks or iOS 7 with that same app.
How is it possible ?
Xcode embeds a small Swift runtime library within your app’s bundle. Because the library is embedded, your app uses a consistent version of Swift that runs on past, present, and future OS releases.
Why should I trust this answer ?
Because I am not saying this answer as one apple guy told me in twitter or I wrote hello world and tested it.
I took it from apple developer blog.
so you can trust this.