Swift: Undefined Symbols: iTunesApplication

二次信任 提交于 2019-12-23 21:29:00

问题


I'm trying to create Swift OS X app now, and found difficulty using ScriptingBridge.

I included proper iTunes.h file, and Xcode is not giving any error when I wrote "iTunesApplication" as type.

However, when I compile(run) the app, it gives me error :( Does anybody know about this issue?

Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_iTunesApplication", referenced from:
__TFC12LoveYouChloe11AppDelegate10showWindowfS0_FPSs9AnyObject_T_ in AppDelegate.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

And here is my code:

var iTunes: iTunesApplication = SBApplication.applicationWithBundleIdentifier("com.apple.iTunes") as iTunesApplication
iTunes.playpause()

回答1:


The best way to solve this is to take the generated Objective-C Scripting Bridge header and convert it into a native Swift. I wrote a Python script (here) that can do that for you. You can see my answer here for a better explanation of what exactly is going on if you're interested.




回答2:


Instead of

var iTunes: iTunesApplication = SBApplication.applicationWithBundleIdentifier("com.apple.iTunes") 

use

var iTunes: AnyObject = SBApplication.applicationWithBundleIdentifier("com.apple.iTunes")

Sometimes the compiler gets confused when you are accessing two or more properties in a row(no idea why) so you may need to use a temporary variable. E.g.:

Instead of:

let songName: String = iTunes.playlists[0].songs[0].name

Try:

let song: AnyObject = iTunes.playlists[0].songs[0]
let songName = song.name

Or Alternatively the faster (this also works if you declare iTunes as an SBApplication) :

let songName: String = iTunes.valueAtIndex(0, inPropertyWithKey: "playlists").valueAtIndex(0, inPropertyWithKey: "songs").valueForKey("name")

EDIT: You can also generate the .swift headers as specified here: https://github.com/tingraldi/SwiftScripting



来源:https://stackoverflow.com/questions/27395805/swift-undefined-symbols-itunesapplication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!