
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Variables
- Swift - Constants
- Swift - Literals
- Swift - Comments
- Swift Operators
- Swift - Operators
- Swift - Arithmetic Operators
- Swift - Comparison Operators
- Swift - Logical Operators
- Swift - Assignment Operators
- Swift - Bitwise Operators
- Swift - Misc Operators
- Swift Advanced Operators
- Swift - Operator Overloading
- Swift - Arithmetic Overflow Operators
- Swift - Identity Operators
- Swift - Range Operators
- Swift Data Types
- Swift - Data Types
- Swift - Integers
- Swift - Floating-Point Numbers
- Swift - Double
- Swift - Boolean
- Swift - Strings
- Swift - Characters
- Swift - Type Aliases
- Swift - Optionals
- Swift - Tuples
- Swift - Assertions and Precondition
- Swift Control Flow
- Swift - Decision Making
- Swift - if statement
- Swift - if...else if...else Statement
- Swift - if-else Statement
- Swift - nested if statements
- Swift - switch statement
- Swift - Loops
- Swift - for in loop
- Swift - While loop
- Swift - repeat...while loop
- Swift - continue statement
- Swift - break statement
- Swift - fall through statement
- Swift Collections
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift Functions
- Swift - Functions
- Swift - Nested Functions
- Swift - Function Overloading
- Swift - Recursion
- Swift - Higher-Order Functions
- Swift Closures
- Swift - Closures
- Swift-Escaping and Non-escaping closure
- Swift - Auto Closures
- Swift OOps
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift-Overriding
- Swift - Initialization
- Swift - Deinitialization
- Swift Advanced
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Error handling
- Swift - Concurrency
- Swift - Type Casting
- Swift - Nested Types
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift - Function vs Method
- Swift - SwiftyJSON
- Swift - Singleton class
- Swift Random Numbers
- Swift Opaque and Boxed Type
Swift Characters
Swift Characters (Character Literals)
A character in Swift is a single character String literal such as A", "!", c", addressed by the data type Character. Or we can say that the character data type is designed to represent a single Unicode character.
Declaring Character Variable
Following is the syntax to declare a variable with character (char) data type −
var char : Character = "A"
Example of Characters
This example demonstrates how you can create character variable by assigning the character literals. Here, we are creating two characters variables and printing their values:
import Foundation let char1: Character = "A" let char2: Character = "B" print("Value of char1 \(char1)") print("Value of char2 \(char2)")
Output
The output of the above code is:
Value of char1 A Value of char2 B
Illegal Character Initialisation
If we try to store more than one character in a Character type variable or constant, then Swift will not allow that and give an error before compilation.
Example
This example demonstrates an illegal character initialisation:
import Foundation // Following is illegal in Swift let char: Character = "AB" print("Value of char \(char)")
The output of the above code is:
main.swift:4:23: error: cannot convert value of type 'String' to specified type 'Character' let char: Character = "AB"
Empty Character Variable Is Not Allowed
Also, we are not allowed to create an empty Character variable or constant which will have an empty value. If we try to do we will get an error.
Example
This example demonstrates an illegal try of creating empty character variable:
import Foundation // Creating empty Character let char1: Character = "" var char2: Character = "" print("Value of char1 \(char1)") print("Value of char2 \(char2)")
The output of the above code is:
main.swift:4:24: error: cannot convert value of type 'String' to specified type 'Character' let char1: Character = "" ^~ main.swift:5:24: error: cannot convert value of type 'String' to specified type 'Character' var char2: Character = "" ^~
Accessing Characters from Strings
A string represents a collection of Character values in a specified order. So we can access individual characters from the given String by iterating over that string with a for-in loop −
Example
import Foundation // Accessing Characters from Strings using for-in loop for ch in "Hello" { print(ch) }
The output of the above code is:
H e l l o
Concatenating String with Character
In Swift, we can concatenate String with a character using the + and += operators. Both the operators concatenate the given character at the end of the specified string.
While concatenating a string with a character we required an explicit conversion from character to string because Swift enforces strong typing. Also, we are not allowed to concatenate a character(variable) with a string.
Example
Swift program to concatenate a string with a character using the + operator.
import Foundation let str = "Swift" let char: Character = "#" // Concatenating string with character using + operator let concatenatedStr = str + String(char) print(concatenatedStr)
The output of the above code is:
Swift#
Example
Swift program to concatenate a string with a character using the += operator.
import Foundation var str = "Swift" let char: Character = "!" // Concatenating string with character using += operator str += String(char) print(str)
The output of the above code is:
Swift!