Skip to content

Instantly share code, notes, and snippets.

@grosch
grosch / IntegerTextField.swift
Created December 21, 2020 11:23
SwiftUI TextField which only accepts integers and binds to an Int? instead of a String
import SwiftUI
private class IntegerTextFieldValue: ObservableObject {
@Published var value = "" {
didSet {
let numbersOnly = value.filter { $0.isNumber }
if value != numbersOnly {
value = numbersOnly
}
}
@grosch
grosch / ChildContext.swift
Last active November 5, 2017 06:02
Create/edit in a child context
import UIKit
import CoreData
class Ticket: NSManagedObject { }
class TicketListViewController: UITableViewController {
var persistentCoordinator: NSPersistentContainer!
var tickets: [Ticket]!
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
@grosch
grosch / SegueHandlerType.swift
Last active October 28, 2017 01:25
Segue Handler
#if os(iOS)
import UIKit
typealias _ViewController = UIViewController
typealias _StoryboardSegue = UIStoryboardSegue
#else
import Cocoa
typealias _ViewController = NSViewController
typealias _StoryboardSegue = NSStoryboardSegue
#endif
//
// NSManagedObjectContext.swift
// TeamKnect
//
// Created by Scott Grosch on 8/7/15.
// Copyright (c) 2015 Gargoyle Software, LLC. All rights reserved.
//
import UIKit
import CoreData
@grosch
grosch / GSPhotoPicker.swift
Last active August 29, 2015 14:24
Helper class to ask for the user to pick/take a photo.
//
// GSPhotoPicker.swift
// TeamKnect
//
// Created by Scott Grosch on 10/18/14.
// Copyright (c) 2014 Gargoyle Software, LLC. All rights reserved.
//
import UIKit
@grosch
grosch / BoxedSet
Created May 7, 2015 17:48
Set<T> that passes by reference
// Set<T> is a struct in Swift, meaning it passes by value, not reference.
// I had to create this so I could pass a Set to another view controller
// and have it actually modify the Set on the original view controller as
// there was only a Back button, not an OK/Done button in the interface.
func ==<T : Hashable>(lhs: BoxedSet<T>, rhs: BoxedSet<T>) -> Bool {
return lhs.set == rhs.set
}
final class BoxedSet<T : Hashable> : Hashable, CollectionType, ArrayLiteralConvertible {
@grosch
grosch / CoreDataFetchController.swift
Last active October 28, 2017 01:25
Better NSFetchedResultscontroller
//
// CoreDataFetchController.swift
//
// Created by Scott Grosch on 12/18/14.
// Copyright (c) 2014 Gargoyle Software, LLC. All rights reserved.
//
import CoreData
final class CoreDataFetchController<T : NSManagedObject> {
@grosch
grosch / GSRegex.swift
Last active October 17, 2015 03:24
Implement regex in Swift, based off http://nshipster.com/swift-literal-convertible/
import Foundation
public protocol GSRegularExpressionMatchable {
func match(regex: GSRegex) -> Bool
func match(regex: NSRegularExpression) -> Bool
}
public class GSRegex: StringLiteralConvertible {
let regex: NSRegularExpression?