Unichar variable in Swift -


how define unichar type variable in swift likes '\0' '\n' in objective-c ?

in swift,

func previouscharacter() -> unichar {     return "\n"     //error } 

compiler gave me error:

cannot convert return expression of type 'string' expected return type 'unichar'

but in objective-c, more easy directly.

in swift, character literals of type character.

let exclamationmark: character = "!" 

see strings , characters

for advanced readers:

you can extend unichar capability accept character literals:

extension unichar : unicodescalarliteralconvertible {     public typealias unicodescalarliteraltype = unicodescalar      public init(unicodescalarliteral scalar: unicodescalar) {         self.init(scalar.value)     } }   let newline: unichar = "\n" let whitespaces = nscharacterset.whitespaceandnewlinecharacterset()  print("c whitespace \(whitespaces.characterismember(newline))") 

however, note swift literals use 4 bytes while unichar uses 2 bytes, therefore characters truncated when converted unichar.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -