我从unichar
的实例方法NSString
返回characterAtIndex(Int)
类型,我想将其与Swift类型String
进行比较。有一个简单的方法吗?
var str = "#ffffff"
var unichar = (str as NSString).characterAtIndex(0)
var unicharString = // Perform magic
var containsHash = unicharString == "#" // Should return `true`
由于
答案 0 :(得分:9)
使用UnicodeScalar将unichar转换为String或Character(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)
。注意:字符串也有init(count: Int, repeatedValue c: UnicodeScalar)
,但这不适合这种情况。
答案 1 :(得分:2)
var str:String = "#ffffff"
var unichar = str[str.startIndex]
var unicharString = "\(unichar)"
var containsHash = unicharString == "#"