0

I'm grabbing metadata from an MPMediaItem like this:

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))")
}

However, when I get a block of metadata printed the "key" is printed as a numeric value instead of "pcst" as shown in the printout:

-----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)

This is happening for all of the metadata/keys (there are 29 in this particular media item).

Also note that this line of code:

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

causes this error: Could not cast value of type '__NSCFNumber' (0x1b80dcdf0) to 'NSString' (0x1b80edae8).

How can I get the string value for the key ("pcst") ?

wayneh
  • 4,393
  • 9
  • 35
  • 70

2 Answers2

1

May be what you are looking for is identifier property of AVMetadataItem.

for metadata in metaArray{
    print(metadata.identifier ?? "DefaultValue")       
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • It looks like it will work - I just have to parse the string to remove the chars before the "/", remove the "/" and the 4 chars that follow.... – wayneh May 01 '17 at 16:37
  • @wayneh You can use `components(separatedBy:)` with `/` and access the last object from result – Nirav D May 01 '17 at 16:41
  • Got an easy way to strip characters from a string? – wayneh May 01 '17 at 16:55
  • @wayneh There are too many ways, like one I have already suggested in my last comment you can also use subscripting by finding `/` index using `range(of:)` method – Nirav D May 01 '17 at 16:58
  • Yeah, I was hoping for something simple like in Java, C#, and so many other languages... :) – wayneh May 01 '17 at 17:04
  • I mean that Java and other languages make it a lot easier to parse strings and work with substrings (for example) than with Swift and all the indexes.... – wayneh May 01 '17 at 17:40
  • I just got it working - the `identifier` is easier to work with since it's a plain string - thanks for the suggestion! – wayneh May 01 '17 at 17:41
  • Yeah, I feel pretty stupid for missing that...I've stared at the metadata for many hours...D'oh! – wayneh May 01 '17 at 17:52
  • @wayneh It happens :) – Nirav D May 01 '17 at 17:53
  • I've got several other difficult questions if you are bored... ;) – wayneh May 01 '17 at 18:02
0

In case others want to see the code I'm using:

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()
}
wayneh
  • 4,393
  • 9
  • 35
  • 70