Skip to content

Instantly share code, notes, and snippets.

Avatar

Joshua Sullivan JoshuaSullivan

View GitHub Profile
@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 / synonym-search.swift
Created Mar 30, 2020
Uses the NaturalLanguage framework combined with a thesaurus API to replace adjectives in a sentence with synonyms. This is designed to be run in a Swift Playground.
View synonym-search.swift
import UIKit
import NaturalLanguage
import PlaygroundSupport
//: Your secret API key from `https://dictionaryapi.com` goes here.
//: THIS WON'T WORK UNLESS YOU GET A KEY.
let thesaurusKey = ""
//: The string you want to work on.
var testString = "The bright sun set behind the green hills. Thin clouds streaked the red sky."
@JoshuaSullivan
JoshuaSullivan / swift4-kvo-playground.swift
Created May 11, 2018
Here is a playground demonstrating how to set up Swift 4 KVO.
View swift4-kvo-playground.swift
//: # Setting up Swift 4 KVO
import Foundation
//: This class has a pair of properties that can be observied by KVO.
//: - Note: This class *must* inheret from `NSObject` in order to posess the KVO functionality.
class ObservableClass: NSObject {
/// An observable string property. Note that it most be annotated with both "@objc" (expose the property to the
/// Objective-C runtime) and "dynamic" (enables KVO for the property).
@objc dynamic private(set) var stringProperty: String = "Starting string!"
@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 / ObfuscationDecoder.swift
Last active May 6, 2019
This playground shows how to use a RepeatingSequence type to create strong string obfuscation using a multi-byte nonce.
View ObfuscationDecoder.swift
import Foundation
public struct ObfuscationDecoder {
public enum DeobfuscationError: Error {
case invalidStringBytes
}
/// The multi-byte nonce used to encode strings.
private static let nonce: [UInt8] = [199, 152, 254, 45, 85, 241, 134, 185, 22, 249, 182, 208, 43, 176, 143, 252]
@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
@JoshuaSullivan
JoshuaSullivan / core-image-filters.md
Last active Feb 5, 2019
Core Image Filter Details
View core-image-filters.md

Core Image Filters

iOS 12.2 has 207 filters.

Accordion Fold Transition (CIAccordionFoldTransition)

First available: iOS 8

Transitions from one image to another of a differing dimensions by unfolding.

@JoshuaSullivan
JoshuaSullivan / AnalyticsBackEndProtocol.swift
Created Dec 18, 2018
A guide to mocking objects with static methods...
View AnalyticsBackEndProtocol.swift
protocol AnalyticsBackEnd {
static func log(_ name: String)
}
extension Analytics: AnalyticsBackEnd {}
@JoshuaSullivan
JoshuaSullivan / UIImage.Orientation+Correction.swift
Created Dec 3, 2018
An extension on `UIImage.Orientation` to convert it to the corresponding `CGImagePropertyOrientation` case.
View UIImage.Orientation+Correction.swift
extension UIImage.Orientation {
/// Get the equivalent `CGImagePropertyOrientation` enum case.
var cgOrientation: CGImagePropertyOrientation {
switch self {
case .up: return .up
case .down: return .down
case .left: return .left
case .right: return .right
case .upMirrored: return .upMirrored
case .downMirrored: return .downMirrored
@JoshuaSullivan
JoshuaSullivan / natural-language-playground.swift
Last active Aug 2, 2018
This playground demonstrates several features of the NaturalLanguage framework that's new in iOS 12.
View natural-language-playground.swift
//: # Natural Language Framework
//: by Josh Sullivan
import Swift
import NaturalLanguage
//: Create a short string to test.
let str = "I am the very model of a modern major general! 😎"
//: And a longer one. Yay, multi-line String literals!
let longStr = """