I'm getting a unichar
type returned from NSString
's instance method characterAtIndex(Int)
and I want to compare it with a Swift type String
. Is there an easy way to do this?
var str = "#ffffff"
var unichar = (str as NSString).characterAtIndex(0)
var unicharString = // Perform magic
var containsHash = unicharString == "#" // Should return `true`
Thanks
Use UnicodeScalar to convert unichar into String or Character (element of String).
var str = "#ffffff"
var unichar = (str as NSString).characterAtIndex(0)
var unicharString = Character(UnicodeScalar(unichar))
var containsHash = unicharString == "#"
typealias unichar = UInt16
).init(_ v: UInt16)
.init(_ scalar: UnicodeScalar)
.Note: String also has init(count: Int, repeatedValue c: UnicodeScalar)
, but this is not suitable for this case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With