Swift 版本 NativeScript

Posted

技术标签:

【中文标题】Swift 版本 NativeScript【英文标题】:Swift Version NativeScript 【发布时间】:2019-03-31 06:18:33 【问题描述】:

使用 NativeScript 如何在 ios 下运行项目?我在运行 tns run iOS --bundle 时收到这些消息

Webpack compilation complete. Watching for file changes.
Webpack build done!
Copying template files...
Platform ios successfully added. v4.2.0
Executing before-shouldPrepare hook from /Users/Zian/Documents/Projects/NativeScript/Hybrid/hooks/before-shouldPrepare/nativescript-dev-webpack.js
Preparing project...
Executing before-prepareJSApp hook from /Users/Zian/Documents/Projects/NativeScript/Hybrid/hooks/before-prepareJSApp/nativescript-dev-webpack.js
Installing pods...
Analyzing dependencies
Downloading dependencies
Installing Socket.IO-Client-Swift (11.1.3)
Installing StarscreamSocketIO (8.0.7)
Installing Toaster (2.0.4)
[!] Unable to determine Swift version for the following pods:

- `Socket.IO-Client-Swift` does not specify a Swift version and none of the targets (`Hybrid`) integrating it have the `SWIFT_VERSION` attribute set. Please contact the author or set the `SWIFT_VERSION` attribute in at least one of the targets that integrate this pod.
- `StarscreamSocketIO` does not specify a Swift version and none of the targets (`Hybrid`) integrating it have the `SWIFT_VERSION` attribute set. Please contact the author or set the `SWIFT_VERSION` attribute in at least one of the targets that integrate this pod.
- `Toaster` does not specify a Swift version and none of the targets (`Hybrid`) integrating it have the `SWIFT_VERSION` attribute set. Please contact the author or set the `SWIFT_VERSION` attribute in at least one of the targets that integrate this pod.
Unable to apply changes on device: 21FFE2BB-EB0D-48E3-A7AD-28CA08DD21E5. Error is: Command pod failed with exit code 1 Error output: 

[!] Automatically assigning platform `ios` with version `8.0` on target `Hybrid` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.

有人可以帮帮我吗?

【问题讨论】:

已经做了,谢谢 消息非常清楚......“请联系作者或在至少一个集成此 pod 的目标中设置SWIFT_VERSION 属性。”有关详细信息,请参阅 cocoapods 文档。 确保您的 POD 存储库是最新的,并尝试在大多数情况下修复问题的干净构建。 @Manoj 尝试您的解决方案,但仍然没有工作 @FauziyyanThafhanRahman 请将答案标记为已接受,如果它解决了您的问题,或者在您的结果中添加另一条评论。 【参考方案1】:

刚遇到同样的错误,发现从 cocoapods 1.6.0 降级到 1.5.3 为我修复了它。

    sudo gem uninstall cocoapods
    sudo gem install cocoapods -v 1.5.3

【讨论】:

您不能永远坚持使用过时版本的 Cocoapods,请参阅下面的答案以获得真正的修复。 已从使用 toaster 切换到 toasty,现在只要没有其他插件仍在使用 swift 3,就可以升级 cocoapods【参考方案2】:

这里的问题似乎是 Toaster (2.0.4),它似乎是为 Swift 3 开发的。

我这样做是为了解决问题:

    转到 nativescript 项目的主文件夹

    在您的 bash shell 中,执行以下操作:

    % export SWIFT_VERSION=3

    那就照常做吧:

    % tns build ios --bundle

    然后在Xcode中打开项目:

    % cd 平台/ios

    % 打开 *.workspace

您会收到有关 Pod 项目中构建错误的警告,并且可能需要设置您的开发团队,但它应该可以工作。

【讨论】:

或者,您可以在Podfile 的顶部添加ENV['SWIFT_VERSION'] = '3'【参考方案3】:

问题在于 Cocoapods 1.6.x 在涉及 SWIFT_VERSION 时具有不同的行为。以前可以在 Podfile 的 post_install 挂钩中设置 SWIFT_VERSION,但现在 pod install 命令甚至在进入 post_install 之前就失败了。 最好的解决方案是使用已经设置了 SWIFT_VERSION 的 Cocoapod,即如果您遇到错误,请尝试联系 Pod 作者。 同时,作为一种变通方法,您可以在您的<path to App_Resources/iOS/Podfile 文件中添加一个pre_install 脚本,其内容如下:

pre_install do |installer|
    installer.analysis_result.specifications.each do |s|
        if s.name == 'Socket.IO-Client-Swift' || s.name == 'Starscream' || s.name == 'StarscreamSocketIO' || s.name == 'Toaster'
            s.swift_version = '4.2'
        end
    end
end

对于每个 Pod,您可以根据其要求设置不同的 Swift 版本。

【讨论】:

【参考方案4】:

我已经用@K Brown 的回答解决了这个问题。

运行tns run ios --bundle 时出现这些错误 对于 Socket.IO-Client-SwiftStarscreamSocketIOToaster,同时安装了 v1.6.0 和 v1.5.3 的 Cocoapods:

- `<package name>` does not specify a Swift version and none of the targets 
(`<project name>`) integrating it have the `SWIFT_VERSION` attribute set. 
Please contact the author or set the `SWIFT_VERSION` attribute in at least one 
of the targets that integrate this pod. 

以下是使用的命令:

sudo gem uninstall cocoapods

sudo gem install cocoapods -v 1.5.3

rm -Rf platforms

tns install

tns run ios --bundle

【讨论】:

【参考方案5】:

这是真正的修复!你不能永远坚持使用 Cocoapods 1.5.3...

降级到1.5.3 不是一个合适的解决方法,并且在未来将不可持续。事实上,这个错误是由于 Cocoapods 1.6.0 版本中引入的一种新行为,它迫使开发人员为他们的项目设置一个 Swift 版本。事实上,目标是让库开发人员在他们的podspec 中指定一个 Swift 版本,这样库用户就不必手动执行此操作(例如,感谢post_install 脚​​本)。你可以看到my discussion about this with a Cocoapods maintainer here。我确实同意这种行为有点误导,因为我们尝试在post_install 脚本中设置 Swift 版本,但之前返回错误......

事实上,正如维护人员告诉我的,解决此问题的正确方法是在项目级别(而不是在 Pod 级别)设置 Swift 版本。为此,只需在Build Settings 中添加一个新的User Defined Setting,键为SWIFT_VERSION 和值4.0(例如,如果您还使用post_install 脚本在Pod 级别设置版本,则任何值都可以在此处使用)。

长话短说,解决方法是添加此键/值:

请注意,当您使用 NativeScript 时,您可能希望在构建之前通过在项目文件夹中运行命令 export SWIFT_VERSION=4 来设置 Swift 版本。

【讨论】:

这将为整个项目设置 SWIFT_VERSION。不同的 Pod 可能需要不同的 SWIFT_VERSION,因此以这种方式设置可能会导致构建失败。

以上是关于Swift 版本 NativeScript的主要内容,如果未能解决你的问题,请参考以下文章

对于使用Swift的目标,需要正确配置“Swift语言版本”(SWIFT_VERSION)

swift新版本相对于swift1.0版本改动大全

swift新版本相对于swift1.0版本改动大全

将版本更改为 Swift 1.2 而不是 Swift 2.1

如何从 Podfile 检查 swift 版本?

Swift 动态库是不是向后兼容旧的 swift 版本项目