Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a unichar variable in swift?

Tags:

swift

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?

like image 248
turkenh Avatar asked Dec 25 '22 20:12

turkenh


2 Answers

Swift can infer the type and you don't need to cast the String literal to NSString:

var delimitedBy = ",".characterAtIndex(0)
like image 199
vacawama Avatar answered Feb 24 '23 06:02

vacawama


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!
like image 38
Martin R Avatar answered Feb 24 '23 08:02

Martin R