I have a function that requires a unichar parameter. But could not find a nice way to initialize a unichar in swift.
I am using the following way:
var delimitedBy:unichar = ("," as NSString).characterAtIndex(0)
Is there a better way to initialize a unichar in swift?
Swift can infer the type and you don't need to cast the String
literal to NSString
:
var delimitedBy = ",".characterAtIndex(0)
Another possible solution:
var delimitedBy = first(",".utf16)!
(Note that unichar
is a type alias for UInt16
). This works also
with string variables (which of course should not be the empty string).
Update for Swift 2/Xcode 7:
var delimitedBy = ",".utf16.first!
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