This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FetchedResultsTableViewController.swift | |
// SmashtagA5 | |
// | |
// Created by Paul Hegarty on 2/3/17. | |
// Copyright © 2017 Stanford University. All rights reserved. | |
// | |
import UIKit | |
import CoreData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UIApplication *thisApp = [UIApplication sharedApplication]; | |
thisApp.idleTimerDisabled = YES; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CommandResolver { | |
typealias MyBlock = () -> Void | |
private var commandDict:[String : MyBlock]= [String:MyBlock]() | |
init() { | |
self.setUpCommandDict(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
NewerOlder