Ankit Kumar Gupta Ankit Kumar Gupta - 22 days ago 17
iOS Question

Swift3 version for emojilessStringWithSubstitution

My current implementation has :

var emojilessStringWithSubstitution: String {
let emojiPatterns = [UnicodeScalar(0x10000)...UnicodeScalar(0x10FFFF), UnicodeScalar(0x2600)...UnicodeScalar(0x27ff)]
return self.unicodeScalars
.filter { ucScalar in !(emojiPatterns.contains{ $0 ~= ucScalar }) }
.reduce("") { $0 + String($1) }
}


errors that occur are :
1. Integer literals overflow when stored into UInt8
2. when i store the unicode scalar in variables and then try to assign then the error is Binary operator '...' cannot be applied to two 'UnicodeScalar?' operands

Kindly help.

Answer

Modify your code as below,

    var emojilessStringWithSubstitution: String {
    let emojiPatterns = [0x10000...0x10FFFF, 0x2600...0x27ff]
    return self.unicodeScalars
        .filter { ucScalar in !(emojiPatterns.contains{ $0 ~= Int(ucScalar.value) }) }
        .reduce("") { $0 + String($1) }
}
Comments