Skip to content

Instantly share code, notes, and snippets.

View ChrisMarshallNY's full-sized avatar
🐲
Developing in Swift, on Long Island, NY

Chris Marshall ChrisMarshallNY

🐲
Developing in Swift, on Long Island, NY
View GitHub Profile
@ChrisMarshallNY
ChrisMarshallNY / Generics.swift
Created April 13, 2020 13:31
Playground to Accompany the Post on Generics
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> {
@ChrisMarshallNY
ChrisMarshallNY / Extensions-3-iOS-Only.swift
Last active April 13, 2020 13:31
iOS Companion Playground to Accompany the Third Post on Extensions
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: ",")
// }
@ChrisMarshallNY
ChrisMarshallNY / Extensions-2.swift
Last active April 13, 2020 13:32
Companion Playground to the Second Post About Extensions
// 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}" }
@ChrisMarshallNY
ChrisMarshallNY / Extensions.swift
Last active April 13, 2020 13:32
Companion Playground to the Extensions Post
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!"
]
@ChrisMarshallNY
ChrisMarshallNY / ClosuresContextAndCapture-2.swift
Last active April 13, 2020 13:32
Companion Playground to the Closures, Contexts and Capture Post (Part 2)
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)")
@ChrisMarshallNY
ChrisMarshallNY / ClosuresContextAndCapture.swift
Last active April 13, 2020 13:33
Companion Playground to the Closures, Context and Capture Post
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
@ChrisMarshallNY
ChrisMarshallNY / EnumsWithAssociatedValues.swift
Last active April 13, 2020 13:33
Companion Playground to the Post on Enums With Associated Values
/*
# 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.
@ChrisMarshallNY
ChrisMarshallNY / Ranges.swift
Last active April 13, 2020 13:33
Companion Playground to the Post on Ranges
//: 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
@ChrisMarshallNY
ChrisMarshallNY / Functions.swift
Last active April 13, 2020 13:34
Companion Playground to the Post on Functions
/*:
# 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)")
}
@ChrisMarshallNY
ChrisMarshallNY / Playground-0-RuntimeInformation.swift
Last active April 13, 2020 13:35
Playground Accompaniment to the Introductory Swiftwater Post
/*
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)")