Skip to content

Instantly share code, notes, and snippets.

View philosopherdog's full-sized avatar

Steve Thompson philosopherdog

View GitHub Profile

Declaring models

class Dog: Object {

  dynamic var id = ""

  dynamic var name = ""

 dynamic var tempVariable = ""
@philosopherdog
philosopherdog / StringDimensionProvider.swift
Last active December 10, 2021 20:47
String Dimension Provider
//
// StringDimensionProvider.swift
//
// Created by steve on 2019-07-11.
// Copyright © 2019 Steven Thompson. All rights reserved.
//
//
// StringHeightProvider.swift
//
@philosopherdog
philosopherdog / UnwrapEnum.swift
Last active December 8, 2021 17:34
Swift Unwrap Enum Associated Type Without Switch #enum
enum CommunityProfileSection {
case FacilityName(String?)
case ContactInfo(ProfileContactViewModel)
case LocationInfo([ExpandableProfileModel])
}
extension CommunityProfileSection {
var facilityName: String? {
if case let .FacilityName(name) = self {
import UIKit
extension UICollectionReusableView {
/**
Used for creating dynamic height cells
- Call from overridden UICollectionReusableView (Cell) method `preferredLayoutAttributesFitting(_:)` which the system calls
- Setup the cell width in `sizeForItem` as usual & pass a temp height
- Setup Autolayout on cells to ensure a height can be computed
@philosopherdog
philosopherdog / ProtocolWitness.swift
Last active December 5, 2021 21:34
Simple Protocol Witness Example
import UIKit
struct ViewModel1 {
let title = "Hello"
}
struct ViewModel2 {
let message = "some message"
}
@philosopherdog
philosopherdog / Equating.swift
Last active October 13, 2021 16:28
Equatable Type Erasure
protocol Equating {
func isEqualTo(_ other: Equating)-> Bool
}
extension Equating where Self: Equatable {
func isEqualTo(_ other: Equating) -> Bool {
guard let other = other as? Self else { return false }
return other == self
}
}
public struct AnyEquatable: Equatable {
private let value: Any
private let equals: Any -> Bool
public init<E: Equatable>(_ value: E) {
self.value = value
self.equals = { ($0 as? E == value) ?? false }
}
}
@philosopherdog
philosopherdog / CountdownTests.swift
Created August 16, 2021 23:48 — forked from JonnyBeeGod/CountdownTests.swift
This code allows for testing UNNotificationCenter and UNNotificationSettings
func testNotifications() {
// map all authorizationStatus with expected Result
let authorizationStatusMap: [UNAuthorizationStatus: Int] = [.authorized: 1, .denied: 0, .notDetermined: 0, .provisional: 1]
UNNotificationSettings.swizzleAuthorizationStatus()
authorizationStatusMap.forEach { (key: UNAuthorizationStatus, value: Int) in
UNNotificationSettings.fakeAuthorizationStatus = key
let mockCenter = UserNotificationCenterMock()
let mockCoder = MockNSCoder()
import Foundation
final class Sample: NSObject {
@objc dynamic var name: String = ""
}
class MyObj: NSObject {
@objc dynamic var test: String = ""
}
extension NSObjectProtocol where Self: NSObject {
import UIKit
func g(_ b: String) -> String {
b
}
func f(_ s: String?) -> String? {
guard let unwrappedS = s else {
return nil
}