gpt4 book ai didi

ios - Swift 5.1 运行时如何与旧版本的 iOS 一起工作?

转载 作者:可可西里 更新时间:2023-10-31 23:51:13 25 4
gpt4 key购买 nike

大约一年前,如果你想使用 Swift 4.2 进行 iOS 开发,你必须安装 Xcode 10,这意味着你使用的是 iOS 12 SDK。作为应用程序部署的一部分,Swift 4.2 运行时将自动与您的应用程序二进制文件捆绑在一起。这意味着安装您的应用程序的用户实际上会下载该 Swift 运行时的副本,以使您的应用程序正常运行。

但是,Swift 5 带来了 ABI 稳定性,如果您的部署目标是 iOS 12.2,您不再需要捆绑运行时,因为运行时现在是该 iOS 版本的一部分。但是,如果您想支持 iOS 10 和 iOS 11,此 Swift 运行时仍将与您的应用程序二进制文件捆绑在一起,并且其行为方式与上述相同。

有关 swift.org 的文档声明相同:

Apps deploying back to earlier OS releases will have a copy of the Swift runtime embedded inside them. Those copies of the runtime will be ignored — essentially inert — when running on OS releases that ship with the Swift runtime.

到目前为止一切顺利。如果您将 Xcode 10.2 与 Swift 5.0 一起使用,并且将您的应用程序部署到较早的 iOS 版本,您仍然会捆绑 Swift 5.0 运行时。然后,如果您的应用程序在 iOS 12 上运行,应用程序将使用 iOS 提供的运行时,如果它运行在例如iOS 11,它将使用作为应用程序二进制文件的一部分捆绑的运行时。现在是第一个问题:这是一个正确的假设吗?

现在我们来看看将于 9 月发布的 Swift 5.1 和 iOS 13。 Swift 5.1 包括一些额外的运行时特性,例如不透明结果类型,需要 Swift 5.1 运行时。

在 WWDC 2019 session 402“Swift 的新功能”中,演讲者在讨论 Swift 5.1 功能不透明结果类型 (SE-0244) 时提到该功能仅适用于新操作系统:

Requires new Swift runtime support

Available on macOS Catalina, iOS 13, tvOS 13, watchOS 6 and later

这是让我感到困惑的部分。无论您是否支持较早的 iOS 版本(例如 iOS 10),Swift 运行时 5.1 都不会随您的应用一起提供,从而使其能够使用这些新的运行时功能,或者我只是没有正确理解这一点?

最佳答案

Now the first question: Is that a correct assumption?

是的,这是正确的。

Wouldn't Swift runtime 5.1 be shipped with your app regardless if you support older iOS versions (e.g. iOS 10 as well), thus enabling it to use these new runtime features or am I just not understanding this correctly?

嵌入式运行时与操作系统中的运行时并不完全相同。例如。操作系统中的运行时紧密集成:

By being in the OS, the Swift runtime libraries can be tightly integrated with other components of the OS, particularly the Objective-C runtime and Foundation framework. The OS runtime libraries can also be incorporated into the dyld shared cache so that they have minimal memory and load time overhead compared to dylibs outside the shared cache.

来源:https://swift.org/blog/abi-stability-and-apple/

当然,嵌入式运行时无法紧密集成到旧系统中。嵌入式运行时只能支持它正在执行的当前系统上已经可能的功能。当您的应用在旧系统上运行时,需要更新系统的功能根本不存在。

请注意,这对于 ObjC 来说从未有过不同。如果某个类或方法仅从某个操作系统版本开始存在,您仍然可以向后部署到较旧的系统版本,但您无法在那里使用该类/方法,因为它根本不存在。

if (@available(iOS 13, *)) {
// Code requiring iOS 13
} else {
// Alternative code for older OS versions
}

或者在 Swift 中:

if #available(iOS 13, *) {
// Code requiring iOS 13
} else {
// Alternative code for older OS versions
}

就像 ObjC 一样,新的 Swift 特性从现在开始将只适用于新的操作系统。仅当有可能使这些功能也可用于较旧的操作系统时,无论这些操作系统是否附带运行时或需要使用嵌入式操作系统,此功能也可以向后部署,但不一定全部部署。

例如10.15 在其捆绑的运行时中引入了一个新功能,然后也许这个功能也可以使用 shim 库在 10.14 和 10.13 上使用,但不能用于 10.12 到 10.9,然后这个功能将被标记为“需要 macOS 10.13 或更新版本”。

如果部署到 10.15,则无需执行任何操作,因为 10.15 的运行时支持该功能。如果部署到 10.14 或 10.13,则编译器将添加 shim 库(就像添加嵌入式运行时一样),在 10.13 和 10.14 上将使用此库中的代码,而在 10.15 和更高版本上将使用运行时中的代码.如果您部署到早于 10.13 的系统,这没关系,但您不能在这些系统上使用此功能。

当然,如果即使通过嵌入式运行时也可以提供新功能,那么对于所有附带自己的运行时但不支持此功能的系统,当然也可以使用垫片库来提供它,因为然后,shim 库可以使用嵌入式运行时使用的相同代码。

该页面上的最后一个问题解释了有时甚至可以为旧系统提供新功能的能力:

Is there anything that can be done to allow runtime support for new Swift features to be backward deployed to older OSes?

It may be possible for some kinds of runtime functionality to be backward deployed, potentially using techniques such as embedding a “shim” runtime library within an app. However, this may not always be possible. The ability to successfully backward-deploy functionality is fundamentally constrained by the limitations and existing bugs of the shipped binary artifact in the old operating system. The Core Team will consider the backward deployment implications of new proposals under review on a case-by-case basis going forward

来源:https://swift.org/blog/abi-stability-and-apple/

关于ios - Swift 5.1 运行时如何与旧版本的 iOS 一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57648898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com