Filters

Hide filters
Platform
Content Type
Difficulty

All Tutorials · 12 Results

Contained in: Swift Apprentice: Beyond the Basics strings
iOS & Swift

Chapter in Swift Apprentice: Beyond the Basics

Result Builders

…create a domain-specific language and compactly express complex values. In this chapter, you will implement a result builder for making fancy attributed strings…
iOS & Swift

Chapter in Swift Apprentice: Beyond the Basics

Concurrency

…model the whole thing like this: struct Domains: Decodable { let data: [Domain] } struct Domain: Decodable { let attributes: Attributes } struct Attributes: Decodable { let name: String let description: String let level: String } These types store only the attributes that you care about. The types are Decodable because their properties are Decodable…
iOS & Swift

Chapter in Swift Apprentice: Beyond the Basics

Custom Operators, Subscripts & Keypaths

…works: var number = 2 number **= exponent The value number is modified by the **= operator in-place. Mini-exercises Implement a custom multiplication operator for strings so that the following code works: let baseString = "abc" let times = 5 var multipliedString = baseString ** times Implement the corresponding multiplication assignment operator so that…
iOS & Swift

Chapter in Swift Apprentice: Beyond the Basics

Error Handling

…safety. Failable Initializers When you try to initialize an object from external input, it may fail. For example, if you’re converting a String into an Int, there is no guarantee it’ll work. let value = Int("3") // Optional(3) let failedValue = Int("nope") // nil In “Swift Apprentice: Fundamentals - Chapter…
iOS & Swift

Chapter in Swift Apprentice: Beyond the Basics

Memory Management

…tutorials, most of which an editor scrutinizes before you see them. Create a new playground and add the following code: class Tutorial { let title: String var editor: Editor? init(title: String) { self.title = title print("Hello tutorial \(title)!") } deinit { print("Goodbye tutorial \(title)!") } } This class models a tutorial. In addition…
iOS & Swift

Chapter in Swift Apprentice: Beyond the Basics

Encoding & Decoding Types

Decodable protocols. Literally: typealias Codable = Encodable & Decodable Automatic Encoding and Decoding Many of Swift’s types are codable out of the box: Int, String, Date, Array and many other types from the Standard Library and the Foundation framework. If you want your type to be codable, the simplest…
iOS & Swift

Chapter in Swift Apprentice: Beyond the Basics

Pattern Matching

…already seen pattern matching in action. In “Swift Apprentice: Fundamentals - Chapter 4: Advanced Control Flow”, you used a switch statement to match numbers and strings in different cases. That’s a simple example, but there’s a lot more to explore on the topic. You’ll dive deep into…
iOS & Swift

Chapter in Swift Apprentice: Beyond the Basics

Property Wrappers

June 2, 2014, as "2014-06-02". You could enforce this validation by applying a @ValidatedDate annotation as follows: struct Order { @ValidatedDate var orderPlacedDate: String @ValidatedDate var shippingDate: String @ValidatedDate var deliveredDate: String } To achieve this, you define this property wrapper: @propertyWrapper public struct ValidatedDate { private var storage: Date…
iOS & Swift

Chapter in Swift Apprentice: Beyond the Basics

Advanced Protocols & Generics

…already know and have used — it’s merely a concrete type accessed through a protocol. Put this into a playground: protocol Pet { var name: String { get } } struct Cat: Pet { var name: String } In this code, the Pet protocol says that pets must have a name. Then, you defined a concrete…
iOS & Swift

Chapter in Swift Apprentice: Beyond the Basics

Protocol-Oriented Programming

…vastly. Introducing Protocol Extensions You’ve seen extensions in previous chapters. They let you add additional methods and computed properties to a type: extension String { func shout() { print(uppercased()) } } "Swift is pretty cool".shout() Here, you’re extending the String type to add a new method. You can extend…
iOS & Swift

Chapter in Swift Apprentice: Beyond the Basics

Value Types & Reference Types

…keep track of your paint. Start with a simple color and paint abstraction: struct Color: CustomStringConvertible { var red, green, blue: Double var description: String { "r: \(red) g: \(green) b: \(blue)" } } // Preset colors extension Color { static let black = Color(red: 0, green: 0, blue: 0) static let white = Color…
iOS & Swift

Chapter in Swift Apprentice: Beyond the Basics

Access Control, Code Organization & Testing

…banking library by extending the behavior of BasicAccount to make a CheckingAccount: class CheckingAccount: BasicAccount { private let accountNumber = UUID().uuidString class Check { let account: String var amount: Dollars private(set) var cashed = false func cash() { cashed = true } init(amount: Dollars, from account: CheckingAccount) { self.amount = amount self.account = account.accountNumber } } } CheckingAccount…