最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】Xcode编译出错:Module compiled with Swift 4.0.1 cannot be imported in Swift 3.2.3

Xcode crifan 5633浏览 0评论

最新的Xcode 9.2 9C40b中:

去编译之前 swift代码出错:

/Users/crifan/dev/dev_root/daryun/Projects/Qoros/QorosSales/Sourcecode/SalesAppiOS/SalesApp/SearchCustomerViewController.swift:10:8: Module compiled with Swift 4.0.1 cannot be imported in Swift 3.2.3: /Users/crifan/dev/dev_root/daryun/Projects/Qoros/QorosSales/Sourcecode/SalesAppiOS/Carthage/Build/iOS/Cartography.framework/Modules/Cartography.swiftmodule/arm64.swiftmodule

之前就遇到:

【未解决】Xcode换9.2后代码编译报错:Module compiled with Swift 4.0 cannot be imported in Swift 3.2.3 – 在路上

但是没解决。

此处也看到项目配置中的是:

用的是swift 3.2.3的编译器

看来错误原因是:

Carthage去编译出来的库,是用的swift 4.0.1去编译的

此处或许终极的解决办法是:

把此处的swift从3.2.3换成4.0.1?

最后不行再去试试这个办法。

现在再去试试:

rm -rf ~/Library/Caches/org.carthage.CarthageKit/DerivedData
carthage update --platform iOS --no-use-binaries

结果找不到entry?

➜  SalesAppiOS git:(master) carthage update --platform iOS --no-use-binaries
No entry found for dependency –-no-use-binaries in Cartfile.
➜  SalesAppiOS git:(master) carthage --help
Unrecognized command: '--help'. See `carthage help`.
➜  SalesAppiOS git:(master) carthage help
Available commands:
archive           Archives built frameworks into a zip that Carthage can use
bootstrap         Check out and build the project's dependencies
build             Build the project's dependencies
checkout          Check out the project's dependencies
copy-frameworks   In a Run Script build phase, copies each framework specified by a SCRIPT_INPUT_FILE environment variable into the built app bundle
fetch             Clones or fetches a Git repository ahead of time
help              Display general or command-specific help
outdated          Check for compatible updates to the project's dependencies
update            Update and rebuild the project's dependencies
version           Display the current version of Carthage
➜  SalesAppiOS git:(master) carthage help update
Update and rebuild the project's dependencies
[--no-checkout]
skip the checking out of dependencies after updating
[--no-build]
skip the building of dependencies after updating
(ignored if --no-checkout option is present)
[--verbose]
print xcodebuild output inline (ignored if --no-build option is present)
[--log-path (string)]
path to the xcode build output. A temporary file is used by default
[--configuration (string)]
the Xcode configuration to build
(ignored if --no-build option is present)
[--platform (platform)]
the platforms to build for (one of 'all', 'macOS', 'iOS', 'watchOS', 'tvOS', or comma-separated values of the formers except for 'all')
(ignored if --no-build option is present)
[--toolchain (string)]
the toolchain to build with
[--derived-data (string)]
path to the custom derived data folder
[--cache-builds]
use cached builds when possible
[--use-ssh]
use SSH for downloading GitHub repositories
[--use-submodules]
add dependencies as Git submodules
[--no-use-binaries]
check out dependency repositories even when prebuilt frameworks exist, disabled if --use-submodules option is present
(ignored if --no-build or --toolchain option is present)
[--color (color)]
whether to apply color and terminal formatting (one of 'auto', 'always', or 'never')
[--project-directory (string)]
the directory containing the Carthage project
[[]]
the dependency names to update, checkout and build
➜  SalesAppiOS git:(master)

<span style=”font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco;”–<no-use-binaries

换个位置,放在update前面就可以了:

➜  SalesAppiOS git:(master) carthage update --no-use-binaries --platform iOS
*** Fetching Cartography
*** Fetching MGSwipeTableCell
*** Fetching Alamofire
...

然后就是漫长的等待了。

期间切换网络所以报错:

A shell task (/usr/bin/env git fetch –prune –quiet origin refs/tags/*:refs/tags/* +refs/heads/*:refs/heads/* (launched in /Users/crifan/Library/Caches/org.carthage.CarthageKit/dependencies/Kingfisher)) failed with exit code 128:

fatal: unable to access ‘https://github.com/onevcat/Kingfisher.git/‘: LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443

继续重新运行。

Swift 4.0.1 cannot be 3.2.3

Swift 4.0.1 cannot be

swift4 – Module compiled with Swift 4.0 cannot be imported in Swift 3.2.1 – Stack Overflow

那抽空试试,换成4.0.1,看看是否有其他问题

希望不要是代码都要改成最新的4.0.1,那就麻烦了。

去看看build编译期间的log:

*** xcodebuild output can be found in /var/folders/46/2hjxz38n22n3ypp_5f6_p__00000gn/T/carthage-xcodebuild.xLepXw.log

期间调用的swift的编译器是:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc

所以自己去看看此处swiftc版本号是多少

可以看到:

➜  ~ which swiftc
/usr/bin/swiftc
➜  ~ where swiftc
/usr/bin/swiftc
➜  ~ swiftc --version
Apple Swift version 4.0.3 (swiftlang-900.0.74.1 clang-900.0.39.2)
Target: x86_64-apple-macosx10.9
➜  ~ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc --version
Apple Swift version 4.0.3 (swiftlang-900.0.74.1 clang-900.0.39.2)
Target: x86_64-apple-macosx10.9
➜  ~ ll /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/
total 190432
-rwxr-xr-x  1 root  wheel    33K 11 17 16:51 ar
-rwxr-xr-x  1 root  wheel    27K 11 17 16:51 as
-rwxr-xr-x  1 root  wheel    18K 11 17 16:51 asa
-rwxr-xr-x  1 root  wheel   211K 11 17 16:51 bison
-rwxr-xr-x  1 root  wheel   146K 11 17 16:51 bitcode_strip
lrwxr-xr-x  1 root  wheel     5B 12 13 11:53 c++ -> clang
-rwxr-xr-x  1 root  wheel    23K 11 17 16:51 c89
-rwxr-xr-x  1 root  wheel    23K 11 17 16:51 c99
lrwxr-xr-x  1 root  wheel     5B 12 13 11:53 cc -> clang
-rwxr-xr-x  1 root  wheel    65M 11 17 16:51 clang
lrwxr-xr-x  1 root  wheel     5B 12 13 11:53 clang++ -> clang
-rwxr-xr-x  1 root  wheel   117K 11 17 16:51 cmpdylib
-rwxr-xr-x  1 root  wheel   142K 11 17 16:51 codesign_allocate
-rwxr-xr-x  1 root  wheel   3.5M 11 17 16:51 coremlcompiler
-rwxr-xr-x  1 root  wheel   3.3K 11  7 08:28 cpp
-rwxr-xr-x  1 root  wheel    27K 11 17 16:51 ctags
-rwxr-xr-x  1 root  wheel   142K 11 17 16:51 ctf_insert
lrwxr-xr-x  1 root  wheel    13B 12 13 11:53 dsymutil -> llvm-dsymutil
-rwxr-xr-x  1 root  wheel   1.1M 11 17 16:51 dsymutil-classic
-rwxr-xr-x  1 root  wheel   983K 11 17 16:51 dwarfdump
-rwxr-xr-x  1 root  wheel   180K 11 17 16:51 dyldinfo
-rwxr-xr-x  2 root  wheel   564K 11 17 16:51 flex
-rwxr-xr-x  2 root  wheel   564K 11 17 16:51 flex++
lrwxr-xr-x  1 root  wheel     8B 12 13 11:53 gcov -> llvm-cov
-rwxr-xr-x  2 root  wheel   139K 11 17 16:51 gm4
-rwxr-xr-x  1 root  wheel    89K 11 17 16:51 gperf
-rwxr-xr-x  1 root  wheel    64K 11 17 16:51 indent
-rwxr-xr-x  1 root  wheel   134K 11 17 16:51 install_name_tool
-rwxr-xr-x  1 root  wheel   1.8M 11 17 16:51 ld
-rwxr-xr-x  1 root  wheel   230B 11  7 09:38 lex
-rwxr-xr-x  1 root  wheel   151K 11 17 16:51 libtool
-rwxr-xr-x  1 root  wheel    64K 11 17 16:51 lipo
-rwxr-xr-x  1 root  wheel   2.6M 11 17 16:51 llvm-cov
-rwxr-xr-x  1 root  wheel    22M 11 17 16:51 llvm-dsymutil
-rwxr-xr-x  1 root  wheel   2.4M 11 17 16:51 llvm-dwarfdump
-rwxr-xr-x  1 root  wheel   7.7M 11 17 16:51 llvm-nm
-rwxr-xr-x  1 root  wheel   9.1M 11 17 16:51 llvm-objdump
-rwxr-xr-x  1 root  wheel    32K 11 17 16:51 llvm-otool
-rwxr-xr-x  1 root  wheel   551K 11 17 16:51 llvm-profdata
-rwxr-xr-x  1 root  wheel   2.2M 11 17 16:51 llvm-size
-rwxr-xr-x  1 root  wheel   3.5K 11  7 09:41 lorder
-rwxr-xr-x  2 root  wheel   139K 11 17 16:51 m4
-rwxr-xr-x  1 root  wheel   7.4K 11  7 09:40 mig
lrwxr-xr-x  1 root  wheel     7B 12 13 11:53 nm -> llvm-nm
-rwxr-xr-x  1 root  wheel   130K 11 17 16:51 nm-classic
-rwxr-xr-x  1 root  wheel   159K 11 17 16:51 nmedit
lrwxr-xr-x  1 root  wheel    12B 12 13 11:53 objdump -> llvm-objdump
lrwxr-xr-x  1 root  wheel    10B 12 13 11:53 otool -> llvm-otool
-rwxr-xr-x  1 root  wheel   625K 11 17 16:51 otool-classic
-rwxr-xr-x  1 root  wheel   126K 11 17 16:51 pagestuff
lrwxr-xr-x  1 root  wheel     7B 12 13 11:53 ranlib -> libtool
-rwxr-xr-x  1 root  wheel    59K 11 17 16:51 rebase
-rwxr-xr-x  1 root  wheel   196K 11 17 16:51 redo_prebinding
-rwxr-xr-x  1 root  wheel    72K 11 17 16:51 rpcgen
-rwxr-xr-x  1 root  wheel    44K 11 17 16:51 segedit
lrwxr-xr-x  1 root  wheel     9B 12 13 11:53 size -> llvm-size
-rwxr-xr-x  1 root  wheel   117K 11 17 16:51 size-classic
-rwxr-xr-x  1 root  wheel   118K 11 17 16:51 strings
-rwxr-xr-x  1 root  wheel   185K 11 17 16:51 strip
-rwxr-xr-x  1 root  wheel    72M 11 17 16:51 swift
-rwxr-xr-x  1 root  wheel   4.5M 11 17 16:51 swift-build
-rwxr-xr-x  1 root  wheel   346K 11 17 16:51 swift-build-tool
-rwxr-xr-x  1 root  wheel   376K 11 17 16:51 swift-demangle
-rwxr-xr-x  1 root  wheel   4.5M 11 17 16:51 swift-package
-rwxr-xr-x  1 root  wheel   4.5M 11 17 16:51 swift-run
-rwxr-xr-x  1 root  wheel    52K 11 17 16:51 swift-stdlib-tool
-rwxr-xr-x  1 root  wheel   4.5M 11 17 16:51 swift-test
lrwxr-xr-x  1 root  wheel     5B 12 13 11:53 swiftc -> swift
-rwxr-xr-x  1 root  wheel    11M 11 17 16:51 tapi
-rwxr-xr-x  1 root  wheel    32K 11 17 16:51 unifdef
-rwxr-xr-x  1 root  wheel   2.9K 11  7 09:41 unifdefall
-rwxr-xr-x  1 root  wheel    49K 11 17 16:51 unwinddump
-rwxr-xr-x  1 root  wheel   135B 11  7 09:42 yacc
➜  ~ ll /usr/bin/swiftc
-rwxr-xr-x  1 root  wheel    18K 12  2 04:45 /usr/bin/swiftc
➜  ~ ll /usr/bin/swift*
-rwxr-xr-x  1 root  wheel    18K 12  2 04:45 /usr/bin/swift
-rwxr-xr-x  1 root  wheel    18K 12  2 04:45 /usr/bin/swiftc

结果是:

默认的swiftc是4.0.3的版本

所在路径是:/usr/bin/

而此处编译所用的swiftc是在:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/

且是个软连接,真正指向的是同目录下的:

(70多M的)swift

不过这个swift(或swiftc)也同样都是4.0.3

都和上面报错的4.0.1不一致:

估计是:

之前报错的是用之前(9.1 beta)Xcode版本编译的

而此处Xcode是最新的9.2,估计对应内部的swiftc的4.0.3版本

Swift 4.0.1 cannot be 3.2.3

Swift 4.0.3 cannot be 3.2.3

realm-cocoa/CHANGELOG.md at master · realm/realm-cocoa

https://github.com/realm/realm-cocoa/blob/master/CHANGELOG.md

对于此处编译很慢的Realm库来说,人家预编译好的framerowk已经包含了:Swift 3.2.3 and 4.0.3

Swift 4.0.3 vs 3.2.3 差别

等了半天终于编译完成:

➜  SalesAppiOS git:(master) carthage update --no-use-binaries --platform iOS
*** Fetching Cartography
*** Fetching MGSwipeTableCell
*** Fetching Alamofire
*** Fetching TextFieldEffects
*** Fetching SwiftyJSON
*** Fetching KMPlaceholderTextView
*** Fetching realm-cocoa
*** Fetching SwiftHEXColors
*** Fetching SwiftHTTPStatusCodes
*** Fetching ActionSheetPicker-3.0
*** Fetching Kingfisher
*** Fetching Charts
*** Fetching SwiftKeychainWrapper
*** Fetching DLRadioButton
*** Fetching XCGLogger
*** Checking out Cartography at "1.1.0"
*** Checking out DLRadioButton at "1.4.11"
*** Checking out Charts at "v3.0.4"
*** Checking out SwiftKeychainWrapper at "2.2.1"
*** Checking out Kingfisher at "3.13.1"
*** Checking out ActionSheetPicker-3.0 at "2.3.0"
*** Checking out SwiftHTTPStatusCodes at "3.2.0"
*** Checking out SwiftHEXColors at "1.1.1"
*** Checking out realm-cocoa at "v3.0.2"
*** Checking out KMPlaceholderTextView at "1.3.0"
*** Checking out SwiftyJSON at "4.0.0"
*** Checking out TextFieldEffects at "1.4.0"
*** Checking out Alamofire at "4.6.0"
*** Checking out MGSwipeTableCell at "1.6.6"
*** Checking out XCGLogger at "Version_4.0.0"
*** xcodebuild output can be found in /var/folders/46/2hjxz38n22n3ypp_5f6_p__00000gn/T/carthage-xcodebuild.xLepXw.log
*** Building scheme "CoreActionSheetPicker" in ActionSheetPicker-3.0.xcworkspace
*** Building scheme "Alamofire iOS" in Alamofire.xcworkspace
*** Building scheme "Cartography-iOS" in Cartography.xcworkspace
*** Building scheme "Charts" in Charts.xcodeproj
*** Building scheme "DLRadioButton" in DLRadioButton.xcodeproj
*** Building scheme "Kingfisher" in Kingfisher.xcworkspace
*** Building scheme "KMPlaceholderTextView" in KMPlaceholderTextView.xcodeproj
*** Building scheme "MGSwipeTableCell" in MGSwipeTableCell.xcodeproj
*** Building scheme "Realm" in Realm.xcworkspace
*** Building scheme "RealmSwift" in Realm.xcworkspace
*** Building scheme "SwiftHEXColors" in SwiftHEXColors.xcodeproj
*** Building scheme "HTTPStatusCodes" in HTTPStatusCodes.xcodeproj
*** Building scheme "SwiftKeychainWrapper" in SwiftKeychainWrapper.xcodeproj
*** Building scheme "SwiftyJSON iOS" in SwiftyJSON.xcworkspace
*** Building scheme "TextFieldEffects" in TextFieldEffects.xcodeproj
*** Building scheme "XCGLogger (iOS)" in XCGLogger.xcodeproj

然后再去Xcode中编译链接看看

结果出现其他错误:

【已解决】Swift代码JSON出错:Call can throw, but it is not marked with ‘try’ and the error is not handled

然后就真的可以正常编译和链接,解决了此问题了,可以正常启动app了:

【总结】

此处Xcode链接之所以报错:

Module compiled with Swift 4.0.1 cannot be imported in Swift 3.2.3

的原因是:

Xcode从9.1 beta升级到了9.2,导致之前用carthage编译好的库和现在项目设置中所指定的swiftc 3.2.3不兼容了。

解决办法是:

rm -rf ~/Library/Caches/org.carthage.CarthageKit/DerivedData
carthage update --no-use-binaries --platform iOS

即可重新编译出符合要求的库了,然后Xcode正常使用就没问题了。

转载请注明:在路上 » 【已解决】Xcode编译出错:Module compiled with Swift 4.0.1 cannot be imported in Swift 3.2.3

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

  1. 没用的话好多啊
    王花花1年前 (2018-08-12)回复
59 queries in 0.099 seconds, using 18.34MB memory