Skip to content

Instantly share code, notes, and snippets.

Avatar

Joshua Sullivan JoshuaSullivan

View GitHub Profile
@JoshuaSullivan
JoshuaSullivan / GenerateRequest.swft
Created May 12, 2015
Challenge Accepted #2 - Signed API Request
View GenerateRequest.swft
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 Aug 29, 2015
This is a Swift 2.0 Playground that demonstrates some use cases for lazy instantiation of object properties.
View LazyInstantiation.swift
//: # 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 Nov 12, 2016
Better mogenerator Swift templates!
View human.swift.motemplate
import Foundation
@objc(<$managedObjectClassName$>)
public class <$managedObjectClassName$>: _<$managedObjectClassName$> {
// Custom logic goes here.
}
@JoshuaSullivan
JoshuaSullivan / Appearance Proxy iOS 9.1.md
Last active Aug 13, 2016
Properties and methods of UIKit classes which expose customization via UIAppearance.
View Appearance Proxy iOS 9.1.md

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 Apr 8, 2020
Don't use Swift enums to box magic strings! Read the blog post: http://www.chibicode.org/?p=16
View EnumExample.swift
enum NotificationNames: String {
case UserDataChanged: "UserDataChangedNotificationName"
case ReceivedAlert: "ReceivedAlertNotificationName"
case PeanutButterJellyTime: "ItsPeanutButterJellyTimeNotificationName"
}
@JoshuaSullivan
JoshuaSullivan / CForLoopExample.swift
Last active Jun 27, 2019
Don't mourn the removal of --, ++ and the C-style for loop from Swift. Read the blog post: http://www.chibicode.org/?p=24
View CForLoopExample.swift
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 Dec 24, 2015
An Exploration of Capture Semantics. Read the blog post: http://www.chibicode.org/?p=28
View ForgotCaptureSemantics.swift
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 Nov 17, 2016
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.
View RepeatingSequence.swift
//: # 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 Dec 31, 2015
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
View DidSetExample.swift
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 Apr 8, 2019
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.
View FormatterTest.swift
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