Skip to content

Commit

Permalink
Added source code from other repo.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwenderlich committed Aug 7, 2020
1 parent 07032f8 commit 3660ca8
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 11 deletions.
119 changes: 119 additions & 0 deletions 01-beginning-swift-optionals.playground/Contents.swift
@@ -0,0 +1,119 @@
/**
* Copyright (c) 2020 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* Notwithstanding the foregoing, you may not use, copy, modify, merge, publish,
* distribute, sublicense, create a derivative work, and/or sell copies of the
* Software in any work that is designed, intended, or marketed for pedagogical or
* instructional purposes related to programming, coding, application development,
* or information technology. Permission for such use, copying, modification,
* merger, publication, distribution, sublicensing, creation of derivative works,
* or sale is expressly withheld.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


// Creates an optional Int, defaults to nil
var foundIndex: Int?

// You can set an optional to a value...
foundIndex = 42

// Or you can set it to nil!
foundIndex = nil

// Test data
let array = Array(0...100)

// Comment this if you want the program to crash
foundIndex = 42

// Force unwrapping
// Avoid this; it will crash if foundIndex is nil!
let unwrappedIndex: Int = foundIndex!
print(array[unwrappedIndex])

// Safely unwrap with if let
if let unwrappedIndex = foundIndex {
// Only executes this line if foundIndex isn't nil
print(array[unwrappedIndex])
}

// You can also shadow the Int? with an Int w/ the same name
if let foundIndex = foundIndex {
print(array[foundIndex])
}

// Use guard let to exit early from a func if there is no value to unwrap
func printElement() {
guard let foundIndex = foundIndex else {
return
}
// Only executes this line if foundIndex isn't nil
print(array[foundIndex])
}

// Safely unwrap in a functional way with map
foundIndex.map { print($0) }

// Test data
struct Address {
var phoneNumber: String?
}
struct Person {
var address: Address?
}
var person: Person?

// Comment this if you want the program to crash
person = Person(address: Address(phoneNumber: "123-123-1234"))

// Use optional chaining to safely test nested optionals
if let number = person?.address?.phoneNumber {
print(number)
}

// Avoid this; it will crash if any of these optionals are nil!
if let number = person!.address!.phoneNumber {
print(number)
}

person = Person(address: nil)

// Use the nil coalescing operator for a fallback value
let number = person?.address?.phoneNumber ?? "867-5309"
print(number)

// Test data
struct Door {
var doorName: String
public var description: String { return doorName }
}
let frontDoor: Door? = Door(doorName: "Front")
let sideDoor: Door? = Door(doorName: "Side")
let backDoor: Door? = nil

// Returns first non-nil element (or nil if all are nil)
let door = frontDoor ?? sideDoor ?? backDoor
if let door = door {
print(door)
}

let anotherDoor = backDoor ?? Door(doorName: "Trap")

4 changes: 4 additions & 0 deletions 01-beginning-swift-optionals.playground/contents.xcplayground
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios' buildActiveScheme='true'>
<timeline fileName='timeline.xctimeline'/>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 30 additions & 2 deletions 03-beginning-swift-control-flow.playground/Contents.swift
Expand Up @@ -73,15 +73,43 @@ for _ in 1...3 {

print("\nWhere Clause")

for i in 1...10 where i % 2 == 1 {
for i in 1...10 where i.isMultiple(of: 2) {
print("\(i) is an odd number.")
}

print("\nIterating Over Arrays")

let daysOfTheWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
for day in daysOfTheWeek {
print(day, terminator: " ")
print(day)
}

// More functional alternative
daysOfTheWeek.forEach { print($0) }

// Alternative ways to iterate an array, from very manual -> algorithmic
for i in 0 ..< daysOfTheWeek.count {
print("Day \(i): \(daysOfTheWeek[i])")
}

for i in daysOfTheWeek.indices {
print("Day \(i): \(daysOfTheWeek[i])")
}

for day in daysOfTheWeek {
if let i = daysOfTheWeek.lastIndex(of: day) {
print("Day \(i): \(day)")
}
}

i = 0
for day in daysOfTheWeek {
print("Day \(i): \(day)")
i += 1
}

for (i, day) in daysOfTheWeek.enumerated() {
print("Day \(i): \(day)")
}

print("\nIterating Over Dictionaries")
Expand Down
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios' executeOnSourceChanges='true'>
<playground version='5.0' target-platform='ios' buildActiveScheme='true'>
<timeline fileName='timeline.xctimeline'/>
</playground>

This file was deleted.

0 comments on commit 3660ca8

Please sign in to comment.