0

I am in the process of converting a Python program to a macOS app and I am now able to read the Apple Music library directly using the ITunesLibrary Framework. I was reading the exported XML library in python and keyed all my music to the persistentID property, which I still want to do. However, my data file that needs to be converted has the persistentID stored as a string type value because that is how iTunes/Apple Music exports the XML file. The ITunesLibrary Framework retrieves the persistentID as a NSNumber type value.

I did a little digging to see if there was a conversion method between the two so I can run a script on my data file and convert all the persistentID string types to NSNumber types. What I found does not work. It returns nil.

// The two below persistID's are for the same song.
var XMLPID = "7C9C79C87E3BEE04" // <--- persistentID from the XML file
var ITLPID:NSNumber = 8979185659088203268 // <--- persistentID from the ITL Framework

// I found this in another discussion here:
let convertedPID = NumberFormatter().number(from: XMLPID)
print("XML PID = \(XMLPID)")
print("Converted PID = \(convertedPID)")

Appreciate any help in getting this to work.

SouthernYankee65
  • 1,129
  • 10
  • 22
  • Likely duplicate of https://stackoverflow.com/questions/27189338/swift-native-functions-to-have-numbers-as-hex-strings – matt Jun 18 '20 at 18:39

1 Answers1

2

They are the same number, in two representations. The first is hex (base 16). The second is decimal (base 10). Swift provides easy conversion.

let hex = "7C9C79C87E3BEE04"
let num = 8979185659088203268 as NSNumber
let swiftnum = num.uint64Value
UInt64(hex, radix:16) == swiftnum // true
String(swiftnum, radix:16, uppercase:true) == hex // true
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Would it be better to use Int64 rather than Int? I tried NSNumber but it wouldn't work. Int and Int64 have the same result and it matches the ITLPID upon conversion. – SouthernYankee65 Jun 18 '20 at 18:49
  • I'm going to go ahead and delete my answer below now that there is some accompanying code. – SouthernYankee65 Jun 18 '20 at 19:25
  • But UInt64 and NSNumber are not compatible. I'm trying to convert the Hex16 to NSNumber, for direct comparison to what ITL framework is returning. Uint64 and NSNumber are not comparable. – SouthernYankee65 Jun 18 '20 at 19:34
  • In order to compare the two as NSNumbers I added the following declaration:`var convertedPID1 = convertedPID as! NSNumber`. This allows direct comparison. – SouthernYankee65 Jun 18 '20 at 19:46