Swift版本的NativeScript

5

使用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`.


有人能帮我吗?


1
已经完成了,谢谢。 - Fauziyyan Thafhan Rahman
信息很清楚... "请联系作者或在至少一个集成此pod的目标中设置SWIFT_VERSION属性。" 有关详细信息,请参阅CocoaPods文档。 - Ashley Mills
确保您的POD库是最新的,并尝试进行干净的构建,这通常可以解决问题。 - Manoj
@Manoj,我正在尝试你的解决方案,但仍然不起作用。 - Fauziyyan Thafhan Rahman
@FauziyyanThafhanRahman 如果这个答案解决了你的问题,请将其标记为已接受,或者添加另一个评论来分享你的结果。 - drewster
5个回答

14

我遇到了同样的错误,并发现将cocoapods版本从1.6.0降级到1.5.3对我有帮助。

    sudo gem uninstall cocoapods
    sudo gem install cocoapods -v 1.5.3

你不能永远坚持使用过时的Cocoapods版本,查看下面我的答案获取真正的解决方案。 - AnthoPak
已经从使用 Toaster 切换到 Toasty,并且只要没有其他插件仍在使用 Swift 3,现在可以升级 CocoaPods。 - K Brown

11

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

我为了解决这个问题,采取了以下步骤:

  1. 进入nativescript项目的主文件夹。

  2. 在bash shell中执行以下命令:

    % export SWIFT_VERSION=3

  3. 然后进行常规操作:

    % tns build ios --bundle

  4. 然后在Xcode中打开该项目:

    % cd platforms/ios

    % Open *.workspace

您可能会收到有关Pods项目构建错误的警告,并且可能需要设置您的开发团队,但应该可以解决问题。


1
或者,您可以在Podfile的顶部包含ENV['SWIFT_VERSION'] = '3' - joriki

8
问题出现在Cocoapods 1.6.x上,当涉及到SWIFT_VERSION时有不同的行为。以前可以在Podfile的post_install钩子中设置SWIFT_VERSION,但现在即使还没有进入post_install,pod安装命令就会失败。 最好的解决方案是使用已经设置了SWIFT_VERSION的Cocoapod,例如如果你遇到了这个错误,请尝试联系Pod作者。 与此同时,可以通过在您的

4

我已经采纳了 @K Brown 的答案解决了这个问题。

当安装了 Cocoapods 的 v1.6.0 和 v1.5.3 时,运行 tns run ios --bundle 命令时,对于 Socket.IO-Client-SwiftStarscreamSocketIOToaster 出现了以下错误:

- `<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

注意:请保留HTML标记。

2

这里是真正的解决方法!不能永远停留在Cocoapods 1.5.3上...

降级到1.5.3并不是一个适当的解决方法,也不会在未来可持续发展。事实上,这个错误是由Cocoapods 1.6.0版本引入的新行为导致的,该行为强制开发者为他们的项目设置Swift版本。目标实际上是让库开发者在他们的podspec中指定Swift版本,以便库用户不必手动执行此操作(例如,通过post_install脚本)。你可以在这里看到我与Cocoapods维护人员的讨论。我确实同意这种行为有点误导人,因为我们试图在post_install脚本中设置Swift版本,但是在此之前就返回了错误...

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

长话短说,解决方法是添加这个键/值: enter image description here

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


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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接