Skip to content

Instantly share code, notes, and snippets.

View froggomad's full-sized avatar

Kenny Dubroff (froggomad) froggomad

View GitHub Profile
@froggomad
froggomad / USState.swift
Last active May 6, 2021 20:16
All 50 US States, Abbreviations, and arrays of both in one enum
enum USState: String, CaseIterable {
case alabama = "AL"
case alaska = "AK"
case arizona = "AZ"
case arkansas = "AR"
case california = "CA"
case colorado = "CO"
case connecticut = "CT"
case delaware = "DE"
case florida = "FL"
@froggomad
froggomad / PullView.swift
Created May 8, 2021 23:01
pull to change number of lines in text object
VStack {
HStack {
Text(log.name)
.lineLimit(lineLimit) // lineLimit is @State var
.foregroundColor(.black)
.multilineTextAlignment(.leading)
Spacer()
}
.padding(3)
class PropertyOwner {
var house: Home!
}
class Home {
var owner: PropertyOwner!
}
let home = Home()
let propertyOwner = PropertyOwner()
class ListNode {
var value: Int
var next: ListNode?
init(_ val: Int) {
self.value = val
self.next = nil
}
}
extension ListNode {
func forEach() {
var current: ListNode? = self
while current != nil {
print(current!.val)
current = current!.next
}
}
@froggomad
froggomad / reusableRetainTest.swift
Last active May 29, 2021 17:35
Test for retain cycles, reusably
private func assertNoMemoryLeak(_ instance: AnyObject, file: StaticString = #filePath, line: UInt = #line) {
addTeardownBlock { [weak instance] in
XCTAssertNil(instance, "Instance should have been deallocated. Potential retain cycle.", file: file, line: line)
}
}
func testRetainCycle() {
let vc = ViewController()
vc.countIt()
XCTAssertEqual(vc.numberOfTimes, 1)
assertNoMemoryLeak(vc, file: #filePath, line: #line)
}
@froggomad
froggomad / URLSessionAsyncAwaitUnsafe.swift
Last active June 9, 2021 16:36
Swift 5 URLSessionDataTask async/await
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
detach { [weak self] in
await print(self?.getGoogle())
}
}
@froggomad
froggomad / asyncAwait.swift
Last active June 10, 2021 16:24
Swift 5 Async/Await
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
detach { [weak self] in
do {
try await print(self?.getKennyDubroff())
} catch {
import UIKit
class TitledCollectionView: UIView {
private lazy var vStack: UIStackView = {
let stack = UIStackView(arrangedSubviews: [titleLabel, collectionView])
stack.translatesAutoresizingMaskIntoConstraints = false
stack.axis = .vertical
stack.distribution = .fill
stack.alignment = .fill