- Swift - Access Control
- Swift - Generics
- Swift - Protocols
- Swift - Extensions
- Swift - Type Casting
- Swift - Optional Chaining
- Swift - ARC Overview
- Swift - Deinitialization
- Swift - Initialization
- Swift - Inheritance
- Swift - Subscripts
- Swift - Methods
- Swift - Properties
- Swift - Classes
- Swift - Structures
- Swift - Enumerations
- Swift - Closures
- Swift - Functions
- Swift - Dictionaries
- Swift - Sets
- Swift - Arrays
- Swift - Characters
- Swift - Strings
- Swift - Loops
- Swift - Decision Making
- Swift - Operators
- Swift - Literals
- Swift - Constants
- Swift - Tuples
- Swift - Optionals
- Swift - Variables
- Swift - Data Types
- Swift - Basic Syntax
- Swift - Environment
- Swift - Overview
- Swift - Home
Swift Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Swift - Characters
A character in Swift is a single character String pteral, addressed by the data type Character. Take a look at the following example. It uses two Character constants −
let char1: Character = "A" let char2: Character = "B" print("Value of char1 (char1)") print("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 4 will not allow that. Try to type the following example in Swift 4 Playground and you will get an error even before compilation.
// Following is wrong in Swift 4 let char: Character = "AB" print("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 −
// Following is wrong in Swift 4 let char1: Character = "" var char2: Character = "" print("Value of char1 (char1)") print("Value of char2 (char2)")
Accessing Characters from Strings
As explained while discussing Swift 4 s Strings, String represents a collection of Character values in a specified order. So we can access inspanidual characters from the given String by iterating over that string with a for-in loop −
for ch in "Hello" { print(ch) }
When the above code is compiled and executed, it produces the following result −
H e l l o
Concatenating Strings with Characters
The following example demonstrates how a Swift 4 s Character can be concatenated with Swift 4 s String.
var varA:String = "Hello " let varB:Character = "G" varA.append( varB ) print("Value of varC = (varA)")
When the above code is compiled and executed, it produces the following result −
Value of varC = Hello GAdvertisements