gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:31:20 25 4
gpt4 key购买 nike

我正在尝试接收“正在播放”项目的属性数量。根据 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com