如何在 MPMediaItem 中使用 enumerateValues(forProperties:using:) 方法 (Swift 3)
How to use enumerateValues(forProperties:using:) method in MPMediaItem (Swift 3)
我正在尝试接收“正在播放”项目的属性数量。
根据 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,
MPMediaItemPropertyAlbumArtist,
MPMediaItemPropertyTitle,
MPMediaItemPropertyArtist,
MPMediaItemPropertyArtwork,
MPMediaItemPropertyGenre,
MPMediaItemPropertyLyrics,
MPMediaItemPropertyReleaseDate,
MPMediaItemPropertyPlaybackDuration,
MPMediaItemPropertyRating,
MPMediaItemPropertyAssetURL,
MPMediaItemPropertyIsExplicit,
MPMediaItemPropertyIsCloudItem,
MPMediaItemPropertyHasProtectedAsset]
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
我正在尝试接收“正在播放”项目的属性数量。 根据 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,
MPMediaItemPropertyAlbumArtist,
MPMediaItemPropertyTitle,
MPMediaItemPropertyArtist,
MPMediaItemPropertyArtwork,
MPMediaItemPropertyGenre,
MPMediaItemPropertyLyrics,
MPMediaItemPropertyReleaseDate,
MPMediaItemPropertyPlaybackDuration,
MPMediaItemPropertyRating,
MPMediaItemPropertyAssetURL,
MPMediaItemPropertyIsExplicit,
MPMediaItemPropertyIsCloudItem,
MPMediaItemPropertyHasProtectedAsset]
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