基于swift的应用程序能在OS X 10.9 (Mavericks)/iOS 7及更低版本上运行吗?
例如,我有一台运行OS X 10.8 (Mountain Lion)的机器,我想知道我用Swift写的应用程序是否能在它上面运行。
或者我应该有什么创建一个Swift应用程序使用Mac OS?
基于swift的应用程序能在OS X 10.9 (Mavericks)/iOS 7及更低版本上运行吗?
例如,我有一台运行OS X 10.8 (Mountain Lion)的机器,我想知道我用Swift写的应用程序是否能在它上面运行。
或者我应该有什么创建一个Swift应用程序使用Mac OS?
当前回答
Swift使用与Objective-C相同的运行时,甚至可以在同一个应用程序中与Objective-C共存(根据WWDC 2014的主题)。
这将需要使用Xcode 6和新的SDK进行检查/验证,以找到最终答案。
其他回答
我在iPod Touch(第三代)设备上测试了一个基于swift的基本应用程序。基于swift的应用程序似乎无法在iOS 5上运行。但是在iOS 6.x上运行。
下面是我试图在iOS 5.0.1下启动测试应用程序时,调试日志中显示的内容:
dyld: F_ADDFILESIGS failed for /private/var/mobile/Applications/59E31E79-9525-43B0-9DF6-8FEF3C0080F1/SwiftTestApp.app/Frameworks/libswift_stdlib_core.dylib with errno=1
dyld: F_ADDFILESIGS failed for /private/var/mobile/Applications/59E31E79-9525-43B0-9DF6-8FEF3C0080F1/SwiftTestApp.app/Frameworks/libswiftCoreGraphics.dylib with errno=1
dyld: F_ADDFILESIGS failed for /private/var/mobile/Applications/59E31E79-9525-43B0-9DF6-8FEF3C0080F1/SwiftTestApp.app/Frameworks/libswiftDarwin.dylib with errno=1
dyld: F_ADDFILESIGS failed for /private/var/mobile/Applications/59E31E79-9525-43B0-9DF6-8FEF3C0080F1/SwiftTestApp.app/Frameworks/libswiftDispatch.dylib with errno=1
dyld: F_ADDFILESIGS failed for /private/var/mobile/Applications/59E31E79-9525-43B0-9DF6-8FEF3C0080F1/SwiftTestApp.app/Frameworks/libswiftFoundation.dylib with errno=1
dyld: F_ADDFILESIGS failed for /private/var/mobile/Applications/59E31E79-9525-43B0-9DF6-8FEF3C0080F1/SwiftTestApp.app/Frameworks/libswiftObjectiveC.dylib with errno=1
dyld: F_ADDFILESIGS failed for /private/var/mobile/Applications/59E31E79-9525-43B0-9DF6-8FEF3C0080F1/SwiftTestApp.app/Frameworks/libswiftUIKit.dylib with errno=1
dyld: Symbol not found: _OBJC_CLASS_$_NSObject
Referenced from: /private/var/mobile/Applications/59E31E79-9525-43B0-9DF6-8FEF3C0080F1/SwiftTestApp.app/Frameworks/libswift_stdlib_core.dylib
Expected in: /usr/lib/libobjc.A.dylib
in /private/var/mobile/Applications/59E31E79-9525-43B0-9DF6-8FEF3C0080F1/SwiftTestApp.app/Frameworks/libswift_stdlib_core.dylib
对于iOS 6.1.6,应用程序运行良好,没有显示这些错误消息。
这里似乎有很多旧的答案,所以我只是想发布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.
试试下面的代码:
它在没有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
}
快速更新,从2015年2月15日起生效,我们不能将使用iOS 8之前的SDK开发的应用程序提交到应用商店。所以,记住这一点,最好不要担心这个问题,因为许多人都建议用Swift开发的应用程序也可以部署在OS X 10.9和iOS 7.0上。
这是我从apple Swift博客上读到的一篇文章,可能会有帮助:
应用程序兼容性:
如果你写了一个Swift应用程序,你可以相信你的应用程序将来会工作得很好。事实上,你可以用同样的应用程序回到OS X Mavericks或iOS 7。这是可能的,因为Xcode在你的应用程序包中嵌入了一个小的Swift运行时库。因为这个库是嵌入式的,所以你的应用程序使用了一个一致的Swift版本,可以在过去、现在和未来的操作系统版本上运行。
二进制兼容性和框架:
在确保应用程序的运行时兼容性的同时,Swift语言本身将继续发展,二进制接口也将发生变化。为了安全起见,应用程序的所有组件都应该使用相同版本的Xcode和Swift编译器来构建,以确保它们能够协同工作。
This means that frameworks need to be managed carefully. For instance, if your project uses frameworks to share code with an embedded extension, you will want to build the frameworks, app, and extensions together. It would be dangerous to rely upon binary frameworks that use Swift — especially from third parties. As Swift changes, those frameworks will be incompatible with the rest of your app. When the binary interface stabilizes in a year or two, the Swift runtime will become part of the host OS and this limitation will no longer exist.