Skip to content

Instantly share code, notes, and snippets.

View JoshuaSullivan's full-sized avatar

Joshua Sullivan JoshuaSullivan

View GitHub Profile
@JoshuaSullivan
JoshuaSullivan / GenerateRequest.swft
Created May 12, 2015 15:57
Challenge Accepted #2 - Signed API Request
let inputDictionary = [
"author_name": "Robert Jordan",
"book_title": "Knife of Dreams",
"series": "The Wheel of Time, Book 11",
"publisher": "Tor Fantasy",
"published_date": "November 28, 2006"
]
let sortedKeys = inputDictionary.keys.array.sorted(<)
var queryTerms = Array<String>()
@JoshuaSullivan
JoshuaSullivan / LazyInstantiation.swift
Last active August 29, 2015 14:26
This is a Swift 2.0 Playground that demonstrates some use cases for lazy instantiation of object properties.
//: # Lazy Instantiation
//:
//: This playground demonstrates situations where lazily instantiating properties is valuable.
//: **NOTE:** This is a Swift 2.0 playground and must be opened in Xcode 7.
import UIKit
import CoreImage
//: A helper function to produce a random CGFloat in the range 0..<1
func randomCGFloat() -> CGFloat {
@JoshuaSullivan
JoshuaSullivan / human.swift.motemplate
Last active November 12, 2016 20:55
Better mogenerator Swift templates!
import Foundation
@objc(<$managedObjectClassName$>)
public class <$managedObjectClassName$>: _<$managedObjectClassName$> {
// Custom logic goes here.
}
@JoshuaSullivan
JoshuaSullivan / Appearance Proxy iOS 9.1.md
Last active August 13, 2016 17:26
Properties and methods of UIKit classes which expose customization via UIAppearance.

UIActivityIndicatorView

@property (nullable, readwrite, nonatomic, strong) UIColor *color NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

UIBarButtonItem

- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

- (nullable UIImage *)backgroundImageForState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
@JoshuaSullivan
JoshuaSullivan / EnumExample.swift
Last active April 8, 2020 18:03
Don't use Swift enums to box magic strings! Read the blog post: http://www.chibicode.org/?p=16
enum NotificationNames: String {
case UserDataChanged: "UserDataChangedNotificationName"
case ReceivedAlert: "ReceivedAlertNotificationName"
case PeanutButterJellyTime: "ItsPeanutButterJellyTimeNotificationName"
}
@JoshuaSullivan
JoshuaSullivan / CForLoopExample.swift
Last active June 27, 2019 16:19
Don't mourn the removal of --, ++ and the C-style for loop from Swift. Read the blog post: http://www.chibicode.org/?p=24
let baseString = "/Documents/"
let words = ["Alpha", "Beta", "Gamma", "Delta"]
var paths : [String] = []
for (var i = 0; i < words.count; ++i) {
let word = words[i]
paths.append("\(baseString)\(word)")
}
print(paths)
@JoshuaSullivan
JoshuaSullivan / ForgotCaptureSemantics.swift
Last active December 24, 2015 22:53
An Exploration of Capture Semantics. Read the blog post: http://www.chibicode.org/?p=28
func attemptLogin(user: String, password: String) {
self.loginRequest = APIClient.sharedClient().createLoginRequest(user:user, password:password) {
result in
switch result {
case .Success(let data):
parseLoginData(data) // Compiler error: implicit reference to self
case .Failure(let error):
errorHandlingMethod(error) // Compiler error: implicit reference to self
}
}
@JoshuaSullivan
JoshuaSullivan / RepeatingSequence.swift
Last active November 17, 2016 17:14
This is a simple Sequence type that accepts an array and sequentially returns the elements, looping back to the first as needed until the required number of elements has been generated.
//: # RepeatingSequence
import Swift
import Foundation
struct RepeatingSequence<T>: Sequence {
/// The Collection that we base our sequence on. We use a Collection and not
/// another Sequence because Sequences are not guaranteed to be repeatedly iterated.
let data: AnyCollection<T>
@JoshuaSullivan
JoshuaSullivan / DidSetExample.swift
Last active December 31, 2015 17:48
Swift's didSet property observer is a great way to dynamically configure a view at runtime, but there are limits to what you should do with it. Read the blog post here: http://www.chibicode.org/?p=32
class MyClass {
@IBOutlet weak var outputLabel: UILabel! {
didSet {
// Ensure that the label wasn't just set to nil.
guard let outputLabel = self.outputLabel else { return }
// Set the text color based on the user's style choices.
outputLabel.textColor = StyleManager.sharedManager().outputLabelColor
// Set the label to use fixed-width numbers.
@JoshuaSullivan
JoshuaSullivan / FormatterTest.swift
Last active April 8, 2019 18:33
Here is some example code that demonstrates the penalty incurred with creating a NSDateFormatter every time you need to format a date instead of creating it once and storing it for use when needed.
import Foundation
import QuartzCore
public func testWithMultipleInstantiation() -> CFTimeInterval {
var dateStrings: [String] = []
dateStrings.reserveCapacity(100000)
let start = CACurrentMediaTime()
for _ in 0..<100000 {
let df = NSDateFormatter()
df.dateStyle = .MediumStyle