无法从元数据中获取键值作为字符串

Can't get key value as String from metadata

我正在像这样从 MPMediaItem 中获取元数据:

let url = item.value(forProperty:MPMediaItemPropertyAssetURL) as? NSURL
let asset = AVURLAsset(url: url! as URL, options: nil)
let metaArray = asset.metadata
for metadata in metaArray{
    print("-----metadata:\(metadata)")
    print("-----metadata.key:\(String(describing: metadata.key))")
}

但是,当我打印一块元数据时,"key" 被打印为数值而不是 "pcst",如打印输出所示:

-----metadata:<AVMetadataItem: 0x1740153f0, identifier=itsk/pcst, keySpace=itsk, key class = __NSCFNumber, key=pcst, commonKey=(null), extendedLanguageTag=(null), dataType=com.apple.metadata.datatype.int8, time={INVALID}, duration={INVALID}, startDate=(null), extras={ dataLength = 1; dataType = 21; dataTypeNamespace = "com.apple.itunes"; }, value=1> -----metadata.key:Optional(1885565812)

所有 metadata/keys(此特定媒体项目中有 29 个)都在发生这种情况。

还要注意这行代码:

let realString = NSString(string: metadata.key! as! String)

导致此错误: Could not cast value of type '__NSCFNumber' (0x1b80dcdf0) to 'NSString' (0x1b80edae8).

如何获取键 ("pcst") 的字符串值?

您要找的可能是 AVMetadataItemidentifier 属性。

for metadata in metaArray{
    print(metadata.identifier ?? "DefaultValue")       
}

如果其他人想看到我正在使用的代码:

func returnKeyString(_ inVal: String)->String{
    // expecting the metadata for "identifier" as input - returns key value
    // eg "itsk/ldes" ->  "ldes"
    // or "id3/%00WFD" etc.  -> "wfd"
    var sFinal:String = ""

    if (inVal.contains("/")){
        sFinal = (inVal.components(separatedBy: "/")[1])
    }
    if sFinal.contains("%"){
        sFinal = sFinal.components(separatedBy: "%")[1]
        let index1 = sFinal.index(sFinal.startIndex, offsetBy: 2)
        sFinal = sFinal.substring(from: index1)
    }
    return sFinal.lowercased()
}