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 {} } | |
} |
/* | |
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)") |
/*: | |
# 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)") | |
} |
//: 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 |
/* | |
# 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. |
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 |
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)") |
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!" | |
] |
// 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}" } |