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

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

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


当前回答

Swift应用程序似乎不能在OS X 10.7上运行。我刚刚创建了一个简单的GUI应用程序(一个视图、一个标签、一个按钮),它在Mavericks上运行良好。基本SDK设置为10.9,部署目标设置为10.7。我把这个应用程序从DerivedData文件夹复制到我的10.7虚拟机,它在启动时崩溃了,显示这个错误:

Crashed Thread:  0

Exception Type:  EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000002, 0x0000000000000000

Application Specific Information:
dyld: launch, loading dependent libraries

    Dyld Error Message:

  Library not loaded: /System/Library/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics
  Referenced from: /Users/USER/Desktop/Swift-Test.app/Contents/MacOS/../Frameworks/libswiftAppKit.dylib
  Reason: image not found

Binary Images:
       0x109c65000 -        0x109c6afff +private.Swift-Test (1.0 - 1) <649695D0-58FD-3D02-9176-2D40D4E711F2> /Users/USER/Desktop/Swift-Test.app/Contents/MacOS/Swift-Test
       0x109c83000 -        0x109dbffff +libswift_stdlib_core.dylib (1.0 - 600.0.34.4.5) <10AAC369-9404-321D-A892-49F65856D7AF> /Users/USER/Desktop/Swift-Test.app/Contents/Frameworks/libswift_stdlib_core.dylib
...

然而,这个消息是令人恼火的,因为在这个VM中肯定有一个CoreGraphics库。其他大量使用CoreGraphics的应用程序也可以正常工作。

其他回答

试试下面的代码:

它在没有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
}

我刚刚为你测试了一下,Swift应用程序编译成标准二进制文件,可以在OS X 10.9和iOS 7上运行。


简单的Swift应用程序用于测试:

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
}

Swift应用程序似乎不能在OS X 10.7上运行。我刚刚创建了一个简单的GUI应用程序(一个视图、一个标签、一个按钮),它在Mavericks上运行良好。基本SDK设置为10.9,部署目标设置为10.7。我把这个应用程序从DerivedData文件夹复制到我的10.7虚拟机,它在启动时崩溃了,显示这个错误:

Crashed Thread:  0

Exception Type:  EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000002, 0x0000000000000000

Application Specific Information:
dyld: launch, loading dependent libraries

    Dyld Error Message:

  Library not loaded: /System/Library/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics
  Referenced from: /Users/USER/Desktop/Swift-Test.app/Contents/MacOS/../Frameworks/libswiftAppKit.dylib
  Reason: image not found

Binary Images:
       0x109c65000 -        0x109c6afff +private.Swift-Test (1.0 - 1) <649695D0-58FD-3D02-9176-2D40D4E711F2> /Users/USER/Desktop/Swift-Test.app/Contents/MacOS/Swift-Test
       0x109c83000 -        0x109dbffff +libswift_stdlib_core.dylib (1.0 - 600.0.34.4.5) <10AAC369-9404-321D-A892-49F65856D7AF> /Users/USER/Desktop/Swift-Test.app/Contents/Frameworks/libswift_stdlib_core.dylib
...

然而,这个消息是令人恼火的,因为在这个VM中肯定有一个CoreGraphics库。其他大量使用CoreGraphics的应用程序也可以正常工作。

虽然代码证实了这一点,但苹果自己在WWDC 2014的技术主题演讲(平台状态,102次会议,大约34分00秒)中表示,Swift将在iOS 7和Mavericks上兼容。

苹果公司宣布Swift应用程序将向后兼容iOS 7和OS X Mavericks。WWDC应用程序是用Swift编写的。