如何使用EnumerateValues(propoperties:使用::) MpMediaItem中的方法(Swift



我正在尝试接收现在播放项目的属性数量。每个苹果文档:

任何时候该应用程序访问多个属性,对一组属性密钥进行列举比获取每个单独属性更有效。

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

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
}

此文档有助于我了解适当的用法 - https://developer.apple.com/library/content/documentation/swift/conceptual/swift_programming_language/closures.html

相关内容

  • 没有找到相关文章

最新更新