I hereby claim:
- I am chrismarshallny on github.
- I am chrismarshallny (https://keybase.io/chrismarshallny) on keybase.
- I have a public key ASA9qgqTBewdJkOVIqdYblw88MkJ0bQoBEjYQDebihAcMQo
To claim this, I am signing this object:
I hereby claim:
To claim this, I am signing this object:
// This is the little demo, at the start. | |
protocol A { | |
var thisIsRequired: String { get } | |
var thisIsOptional: String { get } | |
} | |
extension A { | |
var thisIsOptional: String { get { "" } set {} } | |
} |
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> { |
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: ",") | |
// } |
// 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}" } |
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!" | |
] |
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)") |
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 |
/* | |
# 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. |
//: 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 |