Skip to content

Instantly share code, notes, and snippets.

View JoshuaSullivan's full-sized avatar

Joshua Sullivan JoshuaSullivan

View GitHub Profile
@JoshuaSullivan
JoshuaSullivan / PerlinNoise.cikernel
Last active July 10, 2023 22:51
My attempt at writing a Perlin Noise function.
// Improved Noise - Copyright 2002 Ken Perlin.
// Adapted and updated for iOS by Joshua Sullivan, 2016.01.12
// Apply the function 6t^5 - 15t^4 + 10t^3
float fade(float t)
{
return t * t * t * (t * (t * 6.0 - 15.0) + 10.0);
}
// I'm keeping this around for reference.
@JoshuaSullivan
JoshuaSullivan / Confusion.cikernel
Created January 26, 2016 19:47
A confusing issue with the CIKernel's sample() method. Assigning the result of samplerTransform() to a variable and then using it in the sample() method breaks everything.
// This works.
kernel vec4 simpleFilter(sampler p)
{
vec2 dc = destCoord();
return sample(p, samplerTransform(p, dc));
}
// This does not. It produces [0, 0, 0, 255] for the first 255 pixels and then [0, 0, 0, 0] thereafter.
kernel vec4 simpleFilter(sampler p)
{
@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
@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 / 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 / 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 / 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 / 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 / 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 / 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.
}