I have a AVMetadataItem which has fields encoded in CP1251 (Cyrillic). After reading item.stringValue I get garbage - incorrectly encoded string.
I've tried converting that string to raw UTF8 and then creating a new string using the CP1251 encoding - no luck, result is nil. Tried taking the item.dataValue - no dice, it contains a raw list data (starting with bplist...).
Any ideas are very appreciated.
Thanks in advance.
Swift 2.0 solution:
let origTitleMeta: NSData = (<AVMetadataItem>.timedMetadata?.first?.stringValue?.dataUsingEncoding(NSISOLatin1StringEncoding, allowLossyConversion: true))!
let convertedTitleMeta: String = String(data: origMeta, encoding: NSUTF8StringEncoding)!
I've ended up using Mike Ash's NSPropertyListReader_binary1 and getting the raw data and then mangling it so that NSString would it it's encoding.
Horrible - but worked.
I have AVMetadataItem extension for this:
extension AVMetadataItem {
/// stringValue: ISO-8859-1 → UTF-8
var utf8String: String? {
guard let data = stringValue?.data(using: String.Encoding.isoLatin1, allowLossyConversion: true) else {
return nil
}
return String(data: data as Data, encoding: String.Encoding.utf8)
}
}
Swift 3.0 solution:
let data: Data = item.timedMetadata!.first!.stringValue.data(using: String.Encoding.isoLatin1, allowLossyConversion: true)!
let title: String = String(data: data as Data, encoding: String.Encoding.utf8)!