Skip to content

Instantly share code, notes, and snippets.

View oliverbarreto's full-sized avatar

Oliver oliverbarreto

View GitHub Profile
@oliverbarreto
oliverbarreto / SingletonInSwift4.swift
Created December 8, 2017 16:00
Singleton in Swift 4
// 1. --- The simple version...
class SingletonManager {
static let sharedInstance = SomeManager()
}
// The singleton object will then be accessible through the SingletonManager.sharedInstance() class method.
// 2. --- Or the more complex and secure version...
// [Full credit to](https://cocoacasts.com/what-is-a-singleton-and-how-to-create-one-in-swift/)
class NetworkManager {
@oliverbarreto
oliverbarreto / FetchedResultsTableViewController.swift
Created December 8, 2017 10:07
FetchedResultsTableViewController
//
// FetchedResultsTableViewController.swift
// SmashtagA5
//
// Created by Paul Hegarty on 2/3/17.
// Copyright © 2017 Stanford University. All rights reserved.
//
import UIKit
import CoreData
@oliverbarreto
oliverbarreto / FetchedResultsCollectionViewController.swift
Created December 8, 2017 09:55
FetchedResultsCollectionViewController
import UIKit
import CoreData
class FetchedResultsCollectionViewController: UICollectionViewController, NSFetchedResultsControllerDelegate {
private var sectionChanges = [(type: NSFetchedResultsChangeType, sectionIndex: Int)]()
private var itemChanges = [(type: NSFetchedResultsChangeType, indexPath: IndexPath?, newIndexPath: IndexPath?)]()
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
sectionChanges.append((type, sectionIndex))
@oliverbarreto
oliverbarreto / SimpleStateMachine_v1.swift
Created December 31, 2016 15:29
Simple State Machine v1 (Swift 3)
import Foundation
//: Simple State Machine Delegate Protocol
protocol SimpleStateMachineDelegateProtocol: class {
// Defines the future States of the Machine
associatedtype StateMachineState
// Used to confirm that a state change can occur, and its notification to the delegate to perform somthing upon change
func shouldTransition(fromState: StateMachineState, toState: StateMachineState) -> Bool
@oliverbarreto
oliverbarreto / Swift_Singleton
Created November 1, 2015 18:09
Swift Singleton Pattern
// See this GitHub project for unit tests: https://github.com/hpique/SwiftSingleton
// 1. Class constant
// This approach supports lazy initialization because Swift lazily initializes class constants (and variables), and is thread safe by the definition of let.
// Class constants were introduced in Swift 1.2. If you need to support an earlier version of Swift, use the nested struct approach below or a global constant.
class Singleton {
static let sharedInstance = Singleton()
}
@oliverbarreto
oliverbarreto / preventScreenAutoLock
Created June 24, 2015 11:06
How to programmatically prevent auto-lock iOS?
UIApplication *thisApp = [UIApplication sharedApplication];
thisApp.idleTimerDisabled = YES;
@oliverbarreto
oliverbarreto / SharingData
Created June 15, 2015 13:34
Sharing data between iOS Apps and App Extensions
Sharing data between iOS apps and app extensions
http://www.atomicbird.com/blog/sharing-with-app-extensions
Posted by Tom Harrington on 2014.11.20 in iOS app extensions WatchKit
Since iOS app extensions run as part of a host application rather than as part of their containing app (i.e. your app's extensions run in somebody else's app), data sharing isn't automatic. Finding standard locations like the documents directory doesn't work for shared data. In this post I'll go through the details of how to make it all work.
Along the way I'll get into how to set up real-time messaging between apps and their extensions. Not Cocoa notifications, but a variation of file-based IPC that includes a notification system.
Most of this is not actually specific to iOS extensions, though it's probably more useful with extensions than in other situations.
@oliverbarreto
oliverbarreto / RangeFromArray.swift
Created June 10, 2015 06:05
New Array from Index Range Swift
//Take the first n elements from an array:
func aFunction(numbers: Array<Int>, position: Int) -> Array<Int> {
var newNumbers = Array(numbers[0..<position])
return newNumbers
}
// test
aFunction([1, 2, 3], 2) // returns [1, 2]
@oliverbarreto
oliverbarreto / Adding Swift Closures as values to Swift dictionary.txt
Created June 8, 2015 22:18
Adding Swift Closures as values to Swift dictionary
class CommandResolver {
typealias MyBlock = () -> Void
private var commandDict:[String : MyBlock]= [String:MyBlock]()
init() {
self.setUpCommandDict();
}
@oliverbarreto
oliverbarreto / EnumerateWhenMap.swift
Created June 8, 2015 19:49
You can use enumerate to convert a sequence (Array, String, etc.) to a sequence of tuples with the index and element paired together. That is:
let numbers = [7, 8, 9, 10]
let indexAndNum: [String] = map(enumerate(numbers)) { (index, element) in
return "\(index): \(element)"
}
println(indexAndNum)
// [0: 7, 1: 8, 2: 9, 3: 10]