0

I have a plist which looks like this in a text editor:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <array> <string>\u{1f604}</string> <string>7\u{20e3}</string> <string>\u{1f1ef}\u{1f1f5}</string> ... </array> </plist> 

When I get and print the string, it prints like this:

\u{1f604} 

I suspect the string is somehow invisibly escaped as \\u{1f604}.

How can I convert the string, so it prints the represented character 😀?

2
  • Did you post a comment to my answer, and then removed it? I recall reading it and I was about to get back to it, but it isn't there. Commented Oct 11, 2016 at 1:07
  • Yes. I did. I was mentioning an issue, which I than found out is also handled by the accepted answer. That's why I removed the comment and accepted the working answer. Sorry for confusing you. Commented Oct 11, 2016 at 10:56

2 Answers 2

2

First of all, you need to know that plist editor of Xcode can contain emojis directly:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>emojis</key> <array> <string>😀</string> <string>😂</string> <string>&#x1F601;</string><!-- or you can use numeric reference of XML, in XML editor --> </array> </dict> </plist> 

But, if you already have some escaped version of plist file, you may need to convert it. Using NSRegularExpression, you can write something like this:

class EscapedUnicodeConverter: NSRegularExpression { override func replacementString(for result: NSTextCheckingResult, in string: String, offset: Int, template templ: String) -> String { if result.numberOfRanges == 2, case let nsString = string as NSString, case let matchedString = nsString.substring(with: result.rangeAt(1)), let unicodeScalarValue = UInt32(matchedString, radix: 16), let unicodeScalar = UnicodeScalar(unicodeScalarValue) { return String(unicodeScalar) } else { return super.replacementString(for: result, in: string, offset: offset, template: templ) } } } //Using pattern for Swift let unicodeConverterForSwift = try! EscapedUnicodeConverter(pattern: "\\\\u\\{([0-9A-Fa-f]+)\\}") let origStr = "\\u{1f600}" let result = unicodeConverterForSwift.stringByReplacingMatches(in: origStr, range: NSRange(0..<origStr.utf16.count), withTemplate: "???") print(result) //->😀 
Sign up to request clarification or add additional context in comments.

Comments

0

I think you'll have to parse the string manually:

func toCharacter(_ string: String) -> Character? { guard let start = string.characters.index(of: "{"), let end = string.characters.index(of: "}"), start < end else { return nil } let range = string.index(after: start) ..< end guard let number = Int(string[range], radix: 16), let scalar = UnicodeScalar(number) else { return nil } return Character(scalar) } toCharacter("\\u{1f604}") // "😄" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.