ios - 如何使用 enumerateValues(forProperties :using:) method in MPMediaItem (Swift 3)

标签 ios swift swift3 enumerate mpmediaitem

我正在尝试接收“正在播放”项目的属性数量。 根据 Apple 文档:

Anytime the app accesses more than one property, enumerating over a set of property keys is more efficient than fetching each individual property.

所以我尝试使用他们的方法:

func enumerateValues(forProperties properties: Set<String>, 
               using block: @escaping (String, Any, UnsafeMutablePointer<ObjCBool>) -> Void)

但我就是不明白应该如何使用它。

我的代码:

//MARK: Properties
var allProperties: [String: Any]
var albumTitle: String?
var albumArtist: String?
var title: String?
var artist: String?
var artwork: UIImage?
var genre: String?
var lyrics: String?
var releaseDate: Date?
var playbackDuration: TimeInterval?
var rating: Int?
var assetURL: URL?
var isExplicitItem: Bool?
var isCloudItem: Bool?
var hasProtectedAsset: Bool?

let propertiesSet: Set<String> = [MPMediaItemPropertyAlbumTitle,
                          MPMedia​Item​Property​Album​Artist,
                          MPMediaItemPropertyTitle,
                          MPMedia​Item​Property​Artist,
                          MPMediaItemPropertyArtwork,
                          MPMedia​Item​Property​Genre,
                          MPMedia​Item​Property​Lyrics,
                          MPMedia​Item​Property​ReleaseDate,
                          MPMedia​Item​Property​Playback​Duration,
                          MPMedia​Item​Property​Rating,
                          MPMedia​Item​Property​Asset​URL,
                          MPMediaItemPropertyIs​Explicit,
                          MPMediaItemPropertyIs​Cloud​Item,
                          MPMediaItemPropertyHas​Protected​Asset]

func getAllMetadata() {
    allProperties = nowPlaying?.enumerateValues(forProperties: propertiesSet, 
                                using: //No idea what to put here
        -> [String: Any])
}

如何正确使用?

最佳答案

终于知道怎么用了。所以我重写了我的函数如下:

func getAllMetadata() {
    var allProperties: [String: Any] = [:]
    nowPlaying?.enumerateValues(forProperties: propertiesSet, using: {key,value,_ in allProperties[key] = value})

    albumTitle = allProperties["albumTitle"] as? String
    //and so on
}

This documentation helps me to understand the proper usage - https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html

关于ios - 如何使用 enumerateValues(forProperties :using:) method in MPMediaItem (Swift 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43517955/

相关文章:

swift - 如何以编程方式将自定义 NSToolbarItem 添加到现有工具栏

html - 使用 Alamofire 从其他 viewController 中取出 JSON 中的 HTML 标签

ios - 如何在 NSObject Swift 3.0 的 NSArray 中搜索字符串?

ios - 将点击操作从一个 View 传递到另一个 View

ios - 如何让自定义 UIView 的 subview 出现在 Document Outline 中?

objective-c - 尝试将 Obj-C 代码移植到 Swift 以检查字典中是否存在键以及值是否具有正确的类型

ios - 无法转换 NSSet 类型的值?预期参数类型 Set<HKSampleType>

arrays - 具有通用项的 Swift 数组扩展

ios - 带有 UISegmentedControl 的 UINavigationBar 部分覆盖了 childViews

ios - Swift 4 AppDelegate 线程 1 : breakpoint 1. 2 错误