This is really weird. My app involves a number of unit-of-measure choices made by the user for expressing pressure, temperature and so on. I save these user preferences in UserDefaults and for the most part it all works great. Except for one specific case, when "°F" comes back as "\U00b0F".
The only way I know to see this anomaly is to store the choice of "°F" in an array stored in UserDefaults...
var oldUnit = Central().getKey("tempUnit", defaultValue: "°F") // The user-entered, preferred temperature unit
oldUnits[0] = oldUnit // Puts the stored unit preference into an array with other units choices
defaults.set(oldUnits, forKey: "oldUnits") // Saves the array to UserDefaults
...and then write the userdefaults to a text file to examine the contents:
let filemgr = FileManager.default
let dir: URL = filemgr.urls(for: .documentDirectory, in: .userDomainMask).last! as URL
let url = dir.appendingPathComponent("defaultsFile.txt")
if filemgr.fileExists(atPath: url.path) { // Try to get rid of an existing file
do {
try! filemgr.removeItem(at: url as URL)
} catch {
}
} else {
}
var result = " "
logSB.verbose("Writing the userDefaults to the iCloud defaults file...")
for (key, value) in defaults.dictionaryRepresentation() {
keyStore.set(value, forKey: key)
do {
try "\(key), \(value)".appendLineToURL(fileURL: url as URL) // Creates a new file if one is not pres// logSB.info("Writing \(value) for \(key) to file (not Keystore)")
}
catch {
logSB.error("Could not write to local file")
}
}
do {
result = try String(contentsOf: url as URL, encoding: String.Encoding.utf8)
//
} catch {
logSB.error("Could not read the defaults file after writing it")
}
Any key-value pair set to °F remains that way thorugh this process, but the °F placed into the array and then stored, shows up as \U00b0F.
My app has a rare problem where it crashes at startup after a reinstall. I have no idea if this is the cause of that but it sure is suspicious.
Any ideas?