如何将Objective-C和Swift代码混合在iOS模拟器上?

时间:2022-06-17 16:27:37

I have a set of Swift and Objective-C files that extend some of the functionality in the XCTest framework.

我有一组Swift和Objective-C文件,它们扩展了XCTest框架中的一些功能。

I was easily able to create a Cocoa framework composed of these files. I added all the files to an OS X framework target and, since some of them import XCTest headers, I linked XCTest.framework to my framework. Users are able to add the framework to their unit test targets, provided they are building for OS X.

我很容易创建由这些文件组成的Cocoa框架。我将所有文件添加到一个OS X框架目标中,由于其中一些文件导入了XCTest header,所以我将XCTest.framework链接到我的框架中。用户可以将框架添加到他们的单元测试目标中,只要他们是为OS X构建的。

Question: How can I do the same (i.e.: distribute this code) for users building for the iOS simulator?

问:我怎样才能做同样的事?:分发此代码)为用户构建的iOS模拟器?

What I've Tried (1): Cocoa Touch framework

I can't make a Cocoa Touch framework that imports XCTest.framework--doing so results in the following linker error ("Quick" is the name of the framework):

我无法制作一个Cocoa Touch框架来导入XCTest.framework——这样做会导致以下链接器错误(“Quick”是框架的名称):

Ld /Users/modocache/Library/Developer/Xcode/DerivedData/Quick-cajmiuprocunntdhiwsnsfzqydkc/Build/Products/Debug-iphonesimulator/Quick-iOS.framework/Quick-iOS normal x86_64
    cd /Users/modocache/GitHub/modocache/Quick
    export IPHONEOS_DEPLOYMENT_TARGET=8.0
    export PATH="/Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode6-Beta.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
    /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch x86_64 -dynamiclib -isysroot /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk -L/Users/modocache/Library/Developer/Xcode/DerivedData/Quick-cajmiuprocunntdhiwsnsfzqydkc/Build/Products/Debug-iphonesimulator -F/Users/modocache/Library/Developer/Xcode/DerivedData/Quick-cajmiuprocunntdhiwsnsfzqydkc/Build/Products/Debug-iphonesimulator -F/Applications/Xcode6-Beta.app/Contents/Developer/Library/Frameworks -filelist /Users/modocache/Library/Developer/Xcode/DerivedData/Quick-cajmiuprocunntdhiwsnsfzqydkc/Build/Intermediates/Quick.build/Debug-iphonesimulator/Quick-iOS.build/Objects-normal/x86_64/Quick-iOS.LinkFileList -install_name @rpath/Quick-iOS.framework/Quick-iOS -Xlinker -rpath -Xlinker @executable_path/Frameworks -Xlinker -rpath -Xlinker @loader_path/Frameworks -Xlinker -objc_abi_version -Xlinker 2 -Xlinker -no_implicit_dylibs -mios-simulator-version-min=8.0 -framework XCTest -single_module -compatibility_version 1 -current_version 1 -Xlinker -dependency_info -Xlinker /Users/modocache/Library/Developer/Xcode/DerivedData/Quick-cajmiuprocunntdhiwsnsfzqydkc/Build/Intermediates/Quick.build/Debug-iphonesimulator/Quick-iOS.build/Objects-normal/x86_64/Quick-iOS_dependency_info.dat -o /Users/modocache/Library/Developer/Xcode/DerivedData/Quick-cajmiuprocunntdhiwsnsfzqydkc/Build/Products/Debug-iphonesimulator/Quick-iOS.framework/Quick-iOS

ld: building for iOS Simulator, but linking against dylib built for MacOSX file '/Applications/Xcode6-Beta.app/Contents/Developer/Library/Frameworks/XCTest.framework/XCTest' for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

What I've Tried (2): Cocoa Touch static library

Other projects similar to mine that extend the XCTest framework use static libraries to distribute their code. However, attempting to build a static library that includes Swift code results in the following error:

扩展XCTest框架的其他类似项目使用静态库分发代码。但是,试图构建包含Swift代码的静态库会导致以下错误:

Libtool /Users/MBP-006FYGC/Library/Developer/Xcode/DerivedData/Quick-chbdhbmwzcgjlyfsbxuvmajagydy/Build/Products/Debug/libQuick-OSX.a normal x86_64
    cd /Users/MBP-006FYGC/GitHub/modocache/Quick
    export MACOSX_DEPLOYMENT_TARGET=10.7
    /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool -static -arch_only x86_64 -syslibroot /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -L/Users/MBP-006FYGC/Library/Developer/Xcode/DerivedData/Quick-chbdhbmwzcgjlyfsbxuvmajagydy/Build/Products/Debug -filelist /Users/MBP-006FYGC/Library/Developer/Xcode/DerivedData/Quick-chbdhbmwzcgjlyfsbxuvmajagydy/Build/Intermediates/Quick.build/Debug/Quick-OSX.build/Objects-normal/x86_64/Quick-OSX.LinkFileList -ObjC -L/Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -Xlinker -rpath -Xlinker /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -Xlinker -force_load -Xlinker /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/arc/libarclite_macosx.a -Xlinker -sectalign -Xlinker __SWIFT -Xlinker __ast -Xlinker 4 -Xlinker -sectcreate -Xlinker __SWIFT -Xlinker __ast -Xlinker /Users/MBP-006FYGC/Library/Developer/Xcode/DerivedData/Quick-chbdhbmwzcgjlyfsbxuvmajagydy/Build/Intermediates/Quick.build/Debug/Quick-OSX.build/Objects-normal/x86_64/Quick_OSX.swiftmodule -framework XCTest -framework Foundation -o /Users/MBP-006FYGC/Library/Developer/Xcode/DerivedData/Quick-chbdhbmwzcgjlyfsbxuvmajagydy/Build/Products/Debug/libQuick-OSX.a

error: /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: unknown option character `X' in: -Xlinker
Usage: /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool -static [-] file [...] [-filelist listfile[,dirname]] [-arch_only arch] [-sacLT] [-no_warning_for_no_symbols]
Usage: /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool -dynamic [-] file [...] [-filelist listfile[,dirname]] [-arch_only arch] [-o output] [-install_name name] [-compatibility_version #] [-current_version #] [-seg1addr 0x#] [-segs_read_only_addr 0x#] [-segs_read_write_addr 0x#] [-seg_addr_table <filename>] [-seg_addr_table_filename <file_system_path>] [-all_load] [-noall_load]

More Information

For those that are interested, or if you need more details, the project in question is on GitHub. I'm keeping track of what I've tried so far using this issue.

对于那些感兴趣的人,或者如果您需要更多的细节,所讨论的项目在GitHub上。我一直在用这个问题来记录我到目前为止的尝试。

How can I distribute this Swift/Objective-C code (which uses XCTest code) to users who wish to use it in their iOS unit tests?

如何将这个Swift/Objective-C代码(使用XCTest代码)分发给希望在iOS单元测试中使用它的用户?

1 个解决方案

#1


3  

What is the value of FRAMEWORK_SEARCH_PATHS in your project? I just created a fresh iOS framework and this was the default:

在您的项目中,FRAMEWORK_SEARCH_PATHS的值是多少?我刚刚创建了一个新的iOS框架,这是默认的:

$(inherited) $(DEVELOPER_FRAMEWORKS_DIR)

Try this instead:

试试这个:

$(SDKROOT)/Developer/Library/Frameworks $(inherited) $(DEVELOPER_FRAMEWORKS_DIR)

This should cause Xcode to find the iOS framework bundle before the Mac version.

这将导致Xcode在Mac版本之前找到iOS框架包。

#1


3  

What is the value of FRAMEWORK_SEARCH_PATHS in your project? I just created a fresh iOS framework and this was the default:

在您的项目中,FRAMEWORK_SEARCH_PATHS的值是多少?我刚刚创建了一个新的iOS框架,这是默认的:

$(inherited) $(DEVELOPER_FRAMEWORKS_DIR)

Try this instead:

试试这个:

$(SDKROOT)/Developer/Library/Frameworks $(inherited) $(DEVELOPER_FRAMEWORKS_DIR)

This should cause Xcode to find the iOS framework bundle before the Mac version.

这将导致Xcode在Mac版本之前找到iOS框架包。