基于swift的应用程序能在OS X 10.9 (Mavericks)/iOS 7及更低版本上运行吗?

例如,我有一台运行OS X 10.8 (Mountain Lion)的机器,我想知道我用Swift写的应用程序是否能在它上面运行。

或者我应该有什么创建一个Swift应用程序使用Mac OS?


当前回答

由Leandros发布的回答代码片段似乎有点过时。我已经修复并使其在Swift 5中可编译。

斯威夫特5

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
    self.window = UIWindow(frame: UIScreen.main.bounds)
    let controller = UIViewController()
    let view = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 568))
    view.backgroundColor = UIColor.red
    controller.view = view

    let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
    label.center = CGPoint(x: 160, y: 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
}

其他回答

由Leandros发布的回答代码片段似乎有点过时。我已经修复并使其在Swift 5中可编译。

斯威夫特5

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
    self.window = UIWindow(frame: UIScreen.main.bounds)
    let controller = UIViewController()
    let view = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 568))
    view.backgroundColor = UIColor.red
    controller.view = view

    let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
    label.center = CGPoint(x: 160, y: 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
}

Swift代码可以部署在OS X 10.9和iOS 7.0上。在较旧的操作系统版本上,它通常会在启动时崩溃。

试试下面的代码:

它在没有StoryBoard的情况下工作:

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

    // Create a nav/vc pair using the custom ViewController class

    let nav = UINavigationController()
    let vc = ViewController(nibName: "ViewController", bundle: nil)

    // Push the vc onto the nav
    nav.pushViewController(vc, animated: false)

    // Set the window’s root view controller
    self.window!.rootViewController = nav

    // Present the window
    self.window!.makeKeyAndVisible()
    return true
}

正如Beta 4发布说明所述,Swift应用程序支持iOS 7及以上版本。iOS 6.0, 6.1, 7.0, 7.1, 8.0在Xcode 6 Beta

Swift应用程序支持OS X 10.9及以上平台。“部署目标”中的OS X 10.4 ~ 10.10。我已经在目标10.5到10.10上进行了测试,并在10.9.3上运行

这里似乎有很多旧的答案,所以我只是想发布Swift团队的官方回应。Swift向后兼容OS X Mavericks和iOS 7

苹果开发者swift博客:Objective-C id为swift Any

2014年7月11日

兼容性

我们在WWDC上听到的最常见的问题之一是,“Swift的兼容性故事是什么?”这似乎是一个很好的第一个话题。

App Compatibility Simply put, if you write a Swift app today and submit it to the App Store this Fall when iOS 8 and OS X Yosemite are released, you can trust that your app will work well into the future. In fact, you can target back to OS X Mavericks or iOS 7 with that same app. This is possible because 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.