Swift Overview - Strings

Complexity

A String is made up of Unicodes, but there's also the concept of a Character.

A Character is what a human would perceive to be a single lexical characters. This is true even if it is made up of multiple Unicodes. For example, café might be 5 unicodes (the accent might be separate), but it's 4 Characters,

You can access any character (of type Character) in a String using [] notation. But the indexes inside the [] are not Int, they are a type called String.Index

Given the following is we wanted to get the first char "h". We could use startIndex

let s: String = "hello"

let firstIndex: String.Index = s.startIndex // note that firstIndex's type is not an Int

let firstChar: Character = s[firstIndex] // firstChar = the Character h  

If we wanted the second char.

let secondIndex: String.Index = s.index(after: firstIndex)  
let secondChar: Character = s[secondIndex]  

To jump ahead and get a different index.

let fifthChar: Character = s[s.index(firstIndex, offsetBy: 4)] // fifthChar = o  

We could also use ranges as long as we use String.Indexs.

let substring = s[firstIndex...secondIndex] // substring = "he"  

String Literals

Strings values in swift are built with unicode scalar values so emoji’s can be included in strings
let myString = "mo 💰"

String Interpolation

var cat = “Hobbs"
var hobbsPic = UIImage(named:”\(cat)-pic.jpg”)!
var catFood = “\(cat) eats at least 25 sandwiches a month"

Not Just A String

A String in Swift is a value type (it's a struct). The String struct gives us access to an array of characters by using the “.characters” property

var password = "Meet me in St. Louis"
for character in password.characters {
  if character == “e" {
    print("found an e!")
  } else {
  }
}

A string can be treated as an NSString which a class from objective-c. Swift automatically bridges between the string struct and objective-c’s NSString class so Swift strings have access to NSString methods like replacingOccurrences(of: with:)

let newPassword = password.replacingOccurrences(of: "e", with: "3")  

Variables and Constants

Declaration of variable can be done in two different ways with the var and let keyword.

Var represents a mutable variable and let represents an immutable constant that should not be changed or reassigned even when reassignment would not change the value

var x: Int = 35  
let y = 222  

Variables can by type casted like the above “Int” after the semicolon, but in most cases this is not necessary as swift will be able to infer the type of the variable

Iteration

Even though String is indexable (using []) it's not a collection or a sequence (like an Array). Only sequences and collections can do things like for in or index(of:). Luckily, the characters var in String returns a collection of the String's Characters. With is you can do things like...

for c: Character in s.characters { ... }  

Count

s.characters.count  

Indexing

// a String.Index into the String's characters matches a String.Index into the String
let firstSpace: String.Index = s.characters.index(of: " ")