Skip to content

Instantly share code, notes, and snippets.

View thepost's full-sized avatar

Mike Post thepost

View GitHub Profile
@thepost
thepost / SelectorContext.swift
Created March 29, 2018 23:37
A small protocol to remove the need for your view controllers to use tricky #selector syntax
import UIKit
@objc protocol SelectorContext {
var selector: Selector { get }
func selectorCallback(_ sender: Any)
}
extension UIViewController: SelectorContext {
@thepost
thepost / SelectorDemoViewController.swift
Last active March 30, 2018 14:10
A small extension to demonstrate how to use selectorCallback
class SelectorDemoViewController: UIViewController {
override func viewDidLoad() {
NotificationCenter.default.addObserver(self,
selector: selector,
name: kUnwindNotification,
object: nil)
}
}
@thepost
thepost / PalindromeSolution.swift
Last active May 24, 2018 19:13
Finding a palindrome using a queue and a stack in Swift
public struct Stack<Element>
{
fileprivate var stackItems: [Element] = [Element]()
public init() {}
public mutating func pop() -> Element? {
guard !stackItems.isEmpty else {
return nil
import CoreData
public class ModelController {
typealias VoidCompletion = () -> ()
// MARK: - Properties
private var modelName: String = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as? String ?? ""
extension ModelController {
func add<M: NSManagedObject>(_ type: M.Type) -> M? {
var modelObject: M?
if let entity = NSEntityDescription.entity(forEntityName: M.description(), in: context) {
modelObject = M(entity: entity, insertInto: context)
}
return modelObject
extension ModelController {
func total<M: NSManagedObject>(_ type: M.Type) -> Int {
let entityName = String(describing: type)
let request = NSFetchRequest<M>(entityName: entityName)
do {
let count = try context.count(for: request)
return count
extension ModelController {
func save() {
if context.hasChanges {
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
extension ModelController {
func delete(by objectID: NSManagedObjectID) {
let managedObject = context.object(with: objectID)
context.delete(managedObject)
}
func delete<M: NSManagedObject>(_ type: M.Type, predicate: NSPredicate?=nil) {
@thepost
thepost / Box.tsx
Last active May 3, 2019 15:45
Gist 1 for styled-components tutorial
import React, { FC } from 'react';
import { ViewProps } from 'react-native';
import styled from 'styled-components/native';
import { ThemeInterface } from 'themes/ThemeInterface';
interface IBoxProps extends ViewProps {
theme?: ThemeInterface;
borders?: boolean;
}
import React, { FC } from 'react';
import { ViewProps } from 'react-native';
import styled from 'styled-components/native';
import { ThemeInterface } from 'themes/ThemeInterface';
interface IBoxProps extends ViewProps {
theme?: ThemeInterface;
borders?: boolean;
}