swift - 快速从 Scripting-Bridge 访问 SBElementArray 内容

标签 swift casting itunes scripting-bridge

我一直在尝试在快速代​​码中使用 iTunes.h ScriptingBridge header 中定义的各种 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”的类型转换有关(我认为我需要打包才能访问播放列表内容或无论我想展示什么艺术品),因为下面的代码:

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/

相关文章:

c# - JSON.NET 反序列化为带有 Type 参数的对象

asp.net - 使用 IIS 下运行的 ASP.NET 控制 iTunes

swift - 为什么 dateformatter.date() 默认返回 UTC?

c++ - 常见的 C++ 实现如何处理 static_cast 和 reinterpret_cast

ios - 不允许创建操作

C# 对象到数组

macos - 自动打开目录 (mac os x) 和创建 m3u 播放列表的脚本

C# iTunesLib com : AlbumArtist member not accessible with intellisense

ios - 从 String 获取用于 AVPlayer 的 URL 的问题

swift - 编译器错误 : "Consecutive statements on a line must be separated by ;"