Swift Tutorial - Tutorialspoint

(backadmin) #1

A character in Swift is a single character String literal, addressed by the data type
Character. Take a look at the following example. It uses two Character constants:


import Cocoa

let char1: Character = "A"
let char2: Character = "B"

println("Value of char1 \(char1)")
println("Value of char2 \(char2)")

When the above code is compiled and executed, it produces the following result:


Value of char1 A
Value of char2 B

If you try to store more than one character in a Character type variable or constant, then
Swift will not allow that. Try to type the following example in Swift Playground and you
will get an error even before compilation.


import Cocoa

// Following is wrong in Swift
let char: Character = "AB"

println("Value of char \(char)")

Empty Character Variables


It is not possible to create an empty Character variable or constant which will have an
empty value. The following syntax is not possible:


import Cocoa

// Following is wrong in Swift
let char1: Character = ""
var char2: Character = ""

13. SWIFT – CHARACTERS

Free download pdf