问题
I have searched extensively for an answer to this, but have found none, so I'm asking here. I am trying to read the current track in iTunes using Swift, but whenever I try to reference any of the iTunes classes (iTunesApplication, iTunesTrack, etc) I get the following error:
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_iTunesApplication", referenced from:
__TFC8WAILT_v213iTunesWrapper17getSongWithFormatfS0_FTSS3sepSS10timeOnLeftSb_SS in iTunesWrapper.o
__TMaCSo17iTunesApplication in iTunesWrapper.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Any idea why this is happening?
Thanks,
-tlf
P.S. If I use Obj-C, it works just fine. It only errors when I am using Swift.
回答1:
Because of Swift's strong static typing, it has issues linking with code that it doesn't have either an implementation or a binary for. Therefore to use an Objective-C Bridging Header with it, you will most likely need to use generic SBObject
s because Swift knows the implementation of those, even though your .h declares the other classes.
An alternative is to use a script to generate a native Swift file with declarations that it can see and use. Here is a Python script (full disclosure: it's mine) that generates Objective-C Scripting Bridge headers then creates a native Swift version. This avoids Linker Errors and the aforementioned SBObject generic typing.
来源:https://stackoverflow.com/questions/29135027/swift-itunes-scriptingbridge-linker-error