This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let aStringArray = ["Some String","AnotherString"] // Direct type assignment | |
let myFirstArray = aStringArray // Implicit type assignment, where "aStringArray" is already a String Array. | |
let myDirectArray = [String]() // Explicit empty Array definition, using brackets and an initializer. This is a common way to instantiate an Array. | |
let mySecondArray:[String] = [] // Explicit basic type definition, using brackets. This is the most common form. | |
let myThirdArray:Array<String> = [] // This is the first (explicitly defined) generic form. | |
let myFourthArray = Array<String>() // This is the second (type-assigned) generic form. | |
typealias SwiftGenericType<T> = T | |
struct SwiftGenericStruct<T> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PlaygroundSupport | |
import UIKit | |
import MapKit | |
// This can be messy. It will work, but...ick: | |
extension Array { | |
// ERROR: This will not work | |
// var description: String { | |
// return self.joined(separator: ",") | |
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Now, as of Swift 2, you can add "default vars and funcs" to protocol extensions: | |
protocol EmojiString { | |
var poopEmoji: String { get } | |
var waterEmoji: String { get } | |
var smileyEmoji: String { get } | |
} | |
extension EmojiString { | |
var poopEmoji: String { return "\u{1F4A9}" } | |
var waterEmoji: String { return "\u{1F30A}" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import CoreLocation | |
// Pretend this Dictionary is the result of parsing a "Localizable.strings" file. | |
// In this case, we are translating from Engineering to Marketing. | |
let g_localized_strings: [String: String] = [ | |
"This app's infested with bugs.": "This app's loaded with features!", | |
"What you are asking for is impossible.": "We'll have this ready for beta-testing in a month!", | |
"This project is a nightmare.": "This is the project that I've been dreaming of!", | |
"This schedule is completely unrealistic.": "This sounds like a fascinating challenge!" | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var externalContextVariable: Int = 5 | |
// These are errors | |
//let closure = {[inout externalContextVariable] () -> Void in | |
//let closure = {[var externalContextVariable] () -> Void in | |
let closure = {[externalContextVariable] () -> Void in | |
// This is an error. | |
// externalContextVariable = 6 | |
print("externalContextVariable is \(externalContextVariable)") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var count = 0 | |
print("\nSimple Closure (No capture list):\n") | |
let closure = {print("count is \(count)")} | |
for counter in 0..<5 { | |
count = counter | |
closure() | |
} | |
var optionalCount: Int! = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
# ENUMS | |
Here, we'll play with associated value enums. | |
The scenario is an error reporting system. | |
We have multiple classes of errors, and, within those classes, specific errors. | |
We have a localized error string, and a universal error code for each specific error. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Ranges (Open-Ended) can be empty | |
let rangeIsEmpty = (0..<0).isEmpty | |
//: ClosedRanges cannot be empty. | |
let closedRangeIsEmpty = (0...0).isEmpty | |
//: This will cause a nasty error. Uncomment to see the error. | |
//let downwardSpiral = 1...0 | |
//: Same here (which makes sense). | |
//let downwardSpiral = 1..<0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*: | |
# FUNCTION TYPES AND PARAMETER NAMES | |
[This is the Apple Page on Functions](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html) | |
*/ | |
//: First, we define a function with explicitly named parameters. | |
func thisIsABasicFunction(with: Int, and: Int) { | |
print("\(with), \(and)") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
PLAYGROUND 0: Runtime Information Example | |
©2019 Rift Valley Software. All Rights Reserved. | |
*/ | |
func printInfo(_ inVal: Any) { | |
print("\n\tValue of Parameter:") | |
print("\t\tSimple Value : \(inVal)") | |
print("\t\tSelf : \(inVal.self)") |