我一直试图在swift代码中使用iTunes.h ScriptingBridge头中定义的各种SBElementArray生成器,例如:
播放列表:(SBElementArray<iTunesPlaylist *> *) playlists;
与曲目关联的艺术品列表:(SBElementArray<iTunesArtwork *> *) artworks;
但当我尝试使用与这些数组中包含的类型关联的方法时:

let playlists: SBElementArray = iTunes.playlists()
if let playlist = playlists[0] as? iTunesPlaylist {
    print(playlist.name)
}

我得到一个编译错误:
Undefined symbols for architecture x86_64:
 "_OBJC_CLASS_$_iTunesPlaylist", referenced from:
  objc-class-ref 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)

这似乎仅限于SBElementArray,因为我可以使用以下命令访问当前曲目名称:
let track: iTunesTrack = iTunes.currentTrack;
print(track.name)

我还猜测这与我的代码中从“anyObject”到“iTunesPlaylist”的类型转换(我想我需要成为bale才能访问playlist内容或我想显示的任何艺术作品)有关,因为下面的代码:
let playlists: SBElementArray = iTunes.playlists()
print(playlists[0])
print(type(of: playlists[0]))

核心返回:
<ITunesPlaylist @0x6080000402d0: ITunesPlaylist 0 of application "iTunes" (93931)>
ITunesPlaylist

最佳答案

下载正在尝试创建一个不允许且链接器中不存在的可选iTunesPlaylist并对其进行初始化。强制它成为一个iTunesPlaylist或检查类型。

if playlists.count > 0 {
    let playlist : = playlists[0]
    if playlist is iTunesPlaylist {
        print(playlist.name)
    }
}

关于swift - 从Scripting-Bridge快速访问SBElementArray内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42635808/

10-11 23:14