Skip to content

Instantly share code, notes, and snippets.

Avatar
🚀
Developing in Swift, on Long Island, NY

Chris Marshall ChrisMarshallNY

🚀
Developing in Swift, on Long Island, NY
View GitHub Profile
@ChrisMarshallNY
ChrisMarshallNY / TestCharsetFixedPlayground.swift
Last active Jul 13, 2020
Strange Character Set Comparison Issue
View TestCharsetFixedPlayground.swift
import Foundation
public extension StringProtocol {
func containsOneOfThese(_ inCharacterset: CharacterSet) -> Bool {
self.contains { (char) in
char.unicodeScalars.contains { (scalar) in inCharacterset.contains(scalar) }
}
}
func containsOneOfThese(_ inString: String) -> Bool {
@ChrisMarshallNY
ChrisMarshallNY / UIViewExtension.swift
Created Jun 1, 2020
UIView Extension to Add An Auto-Layout SubView With Equal All-Around Constraints (Fill)
View UIViewExtension.swift
extension UIView {
/* ################################################################## */
/**
This allows us to add a subview, and set it up with auto-layout constraints to fill the superview.
- parameter inSubview: The subview we want to add.
*/
func addContainedView(_ inSubView: UIView) {
addSubview(inSubView)
@ChrisMarshallNY
ChrisMarshallNY / 01-SecondStep-00.swift
Last active Jul 20, 2020
Companion Gists to the Introduction to Core Bluetooth Class
View 01-SecondStep-00.swift
extension ITCB_SDK_Device_Peripheral: CBPeripheralDelegate {
public func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
if let error = error {
print("Encountered an error \(error) for the Peripheral \(peripheral.name ?? "ERROR")")
owner?._sendErrorMessageToAllObservers(error: ITCB_Errors.coreBluetooth(error))
return
}
print("Successfully Discovered \(peripheral.services?.count ?? 0) Services for \(peripheral.name ?? "ERROR").")
peripheral.services?.forEach {
print("Discovered Service: \($0.uuid.uuidString)")
@ChrisMarshallNY
ChrisMarshallNY / 00-StartingPoint-00.swift
Last active Jun 12, 2020
Companion Gists to the Introduction to Core Bluetooth Class
View 00-StartingPoint-00.swift
extension ITCB_SDK_Central {
override var _managerInstance: Any! {
get {
if super._managerInstance == nil {
print("Creating A new instance of CBCentralManager.")
super._managerInstance = CBCentralManager(delegate: self, queue: nil)
}
return super._managerInstance
}
View QuickHashableDemo.swift
struct A<T: Hashable>: Hashable {
static func == (lhs: A<T>, rhs: A<T>) -> Bool { lhs.tVar == rhs.tVar }
let tVar: T
init(_ inVat: T) {
tVar = inVat
}
}
@ChrisMarshallNY
ChrisMarshallNY / NilCoalescing.swift
Created Apr 13, 2020
Companion Playground to the Nil-Coalescing Operator Post
View NilCoalescing.swift
// ##########################################################
// SET UP FOUR CONSTANT ARRAYS OF INT
// ##########################################################
let array1 = [0,1,2,3,4]
let array2 = [5,6,7,8,9]
let array3 = [10,11,12,13,14]
let array4 = [15,16,17,18,19]
// ##########################################################
// WE USE AN IMPLICITLY UNRWAPPED OPTIONAL
@ChrisMarshallNY
ChrisMarshallNY / Some.swift
Created Apr 13, 2020
Companion Playground to the "Some" People Say Post
View Some.swift
protocol P {
func printAhh();
}
// Default implementation
extension P {
func printAhh() {
print("AHHH...")
}
}
@ChrisMarshallNY
ChrisMarshallNY / Parser-0.swift
Created Apr 13, 2020
Companion Repos to the Series on Writing A Parser in Swift
View Parser-0.swift
/*
<containerElement>
<arrayElement>Element Value 01</arrayElement>
<arrayElement>Element Value 02</arrayElement>
<arrayElement>Element Value 03</arrayElement>
<arrayElement>Element Value 04</arrayElement>
<arrayElement>Element Value 05</arrayElement>
</containerElement>
<!--
@ChrisMarshallNY
ChrisMarshallNY / Generics-2.swift
Created Apr 13, 2020
Companion Playground to the Second Post on Generics
View Generics-2.swift
// This is a completely generic protocol. You can use any type for "T".
protocol GenericBaseProtocol {
associatedtype T
var myProperty: T {get set}
init(_ myProperty: T )
}
// You can declare both Comparable and non-Comparable types with this protocol.
// This is Comparable
@ChrisMarshallNY
ChrisMarshallNY / Generics.swift
Created Apr 13, 2020
Playground to Accompany the Post on Generics
View Generics.swift
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> {