Contained in: Swift Apprentice: Fundamentals
strings
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Strings
Apr 23 2025 · Chapter
Text processing is an essential application for any computer language, and String is Swift’s powerhouse type for text handling. Strings are bi-directional collections of Character types that balance correctness, performance and ease…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Types & Operations
Apr 23 2025 · Chapter
…learn about handling different types, including strings that allow you to represent text. You’ll learn about converting between types and get an introduction to type inference, which simplifies your life as a programmer. You’ll learn about tuple types which allow you to group values of any type together…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Regex
Apr 23 2025 · Chapter
…previous chapter, you learned how strings are collections of characters and grapheme clusters, and you learned how to manipulate them. You also learned how to find a character inside a string. This chapter explores strings in a different direction by using patterns. As a human, you can scan a block…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Arrays, Dictionaries & Sets
Apr 23 2025 · Chapter
…much searching you’re planning to do. Mutable Versus Immutable Collections Like the previous types you’ve read about, such as Int or String, when you create a collection, you must declare it as either a constant or a variable. If the collection doesn’t need to change after…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Advanced Classes
Apr 23 2025 · Chapter
…pair of class examples: Person and Student. struct Grade { var letter: Character var points: Double var credits: Double } class Person { var firstName: String var lastName: String init(firstName: String, lastName: String) { self.firstName = firstName self.lastName = lastName } } class Student { var firstName: String var lastName: String var grades: [Grade] = [] init(firstName: String, lastName…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Optionals
Apr 23 2025 · Chapter
…variables and constants you’ve dealt with have concrete values. When you had a string variable, like var name, it had a string value associated with it, like "Matt Galloway". It could have been an empty string, like "", but there was a value to which you could refer. That…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Enumerations
Apr 23 2025 · Chapter
…happen to think of the directions in a different order. “What does 3 mean again?” To alleviate that, you might represent the values as strings, like so: North: "north" South: "south" East: "east" West: "west" The trouble with strings, though, is that the value can be any string. What would…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Generics
Apr 23 2025 · Chapter
…include identifiers, such as names. Adding it lets every value represent the identity of an individual animal or keeper: class Cat { var name: String init(name: String) { self.name = name } } class Dog { var name: String init(name: String) { self.name = name } } class Keeper<Animal> { var name: String init(name: String…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Protocols
Apr 23 2025 · Chapter
…other named type. Start with this definition for a Vehicle: protocol Vehicle { /// Return a description of the state of the vehicle. func describe() -> String // more to come... } Following the keyword, protocol is its name, followed by curly braces with the requirements inside. There is only one method requirement, describe…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Properties
Apr 23 2025 · Chapter
…group related properties and behaviors into a custom type. In the example below, the Car structure has two properties; both are constants that store String values: struct Car { let make: String let color: String } The values inside a structure are called properties. The two properties of Car are stored properties…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Classes
Apr 23 2025 · Chapter
…classes to represent objects. What does values vs. objects mean, though? Creating Classes Consider the following class definition in Swift: class Person { var firstName: String var lastName: String init(firstName: String, lastName: String) { self.firstName = firstName self.lastName = lastName } var fullName: String { "\(firstName) \(lastName)" } } let john = Person(firstName: "Johnny", lastName: "Appleseed") That…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Basic Control Flow
Apr 23 2025 · Chapter
…false values, and how you can use these to compare data. Comparison Operators You’ve seen a few types, such as Int, Double and String. Here you’ll learn about another type that lets you compare values through the comparison operators. When you perform a comparison, such as looking…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Advanced Control Flow
Apr 23 2025 · Chapter
…switch statements also work with data types other than integers. They work with any data type! Here’s an example of switching on a string: let string = "Dog" switch string { case "Cat", "Dog": print("Animal is a house pet.") default: print("Animal is not a house pet.") } This code will…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Collection Iteration With Closures
Apr 23 2025 · Chapter
…trailing closure like so: names.sorted { $0.count > $1.count } // ["ZZZZZZ", "EEEEE", "CCCC", "BB", "A"] Now the array is sorted by the length of the string, with longer strings coming first. Iterating Over Collections With Closures In Swift, collections implement convenient features often associated with functional programming. These features come…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Functions
Apr 23 2025 · Chapter
…printMultipleOf. You can also overload a function name based on a different return type, like so: func getValue() -> Int { 31 } func getValue() -> String { "Matt Galloway" } Here, there are two functions called getValue(), which return different types: one an Int and the other a String. Using these…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Structures
Apr 23 2025 · Chapter
…standard library has many useful types like Int, Double and String. However, it sadly does not include a Pizza type. Structures are types that can store named properties and define actions and behaviors. In this chapter, you will define your custom structure types and begin building a Pizza empire…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Methods
Apr 23 2025 · Chapter
…convert monthsUntilWinterBreak(date:) into a method? let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] struct SimpleDate { var month: String } func monthsUntilWinterBreak(from date: SimpleDate) -> Int { months.firstIndex(of: "December")! - months.firstIndex(of: date.month)! } Note: This example is fragile because it force unwraps an index that…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Expressions, Variables & Constants
Apr 23 2025 · Chapter
…text is represented by a number. You’ll learn more about this in Chapter 2, “Types & Operations”, which delves into types, including strings, the computer term for a text block. Images are no exception. In a computer, each image is also represented by a series of numbers. An image…