Skip to content

Instantly share code, notes, and snippets.

View coryalder's full-sized avatar

Cory Alder coryalder

View GitHub Profile
@coryalder
coryalder / gist:6effee57a123420f2f949955f039fcee
Last active July 11, 2017 18:50 — forked from gavrix/gist:5054182
Script integrating OCLint into XCode. Put it in "Run script" build phase.
#!/bin/bash
# https://gist.github.com/gavrix/5054182 original
source ~/.bash_profile
hash oclint &> /dev/null
if [ $? -eq 1 ]; then
echo >&2 "oclint not found, analyzing stopped"
exit 1
fi
class Dummy: NSObject {
@objc func barStyle() -> UIStatusBarStyle {
return .LightContent
}
func swizzle() {
guard let safariClass = NSClassFromString("SFSafariViewController") else {
print("can't swizzle, iOS 8")
// This code does not compile without the following two changes
//
// 1. add ! to the type of expensiveThing (`String!`)
// 2. expensiveThing = nil // set expensiveThing to nil
//
// This doesn't really make sense to me. Why should I be forced to finish setup, if I'm going to fail?
// The swift book explains that this is required, but not the logic behind it:
// https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-XID_339
// Search for "Failable Initializers for Classes"
//
@coryalder
coryalder / cgi.swift
Created December 3, 2015 23:04
Example linux swift cgi
import Foundation
// work around the currently missing .componentsSeparatedByString method
extension String {
func split(on: Character) -> [String] {
var segments = [String]()
var current = ""
@coryalder
coryalder / SuggestionController.swift
Created November 19, 2015 00:50
Autocomplete filter code from Rivet's autocomplete suggestion engine
// depends on https://github.com/coryalder/LevenshteinSwift
class SuggestionController {
// has a couple of notable vars:
// delegate, where the inputted text is loaded from
// rawCompletions, an array of possible suggestions (unfiltered)
// autocompletions, the filtered array of suggestions we're offer up to the user
func filterAutocompletions() {
@coryalder
coryalder / protocol_id.m
Created July 31, 2015 21:49
id and protocol conformance
// when specify id as the type, you can use NSObject methods.
// when you specify as a custom protocol, you cannot.
// UNLESS you say that your custom protocol includes <NSObject>
#import <Foundation/Foundation.h>
@protocol CustomProtocol // <NSObject> // uncommenting <NSObject> fixes the issue
@end
@coryalder
coryalder / IntroToSwift.swift
Created July 20, 2015 16:47
a quick intro to some of the language features of swift
//: Playground - noun: a place where people can play
import Cocoa
// basic strings
var str: String = "Hello,"
let helloWorld = str + " world"
-(void)viewDidAppear:(BOOL)animated {
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:(CGRect){-50,-50,100,100}];
// 0, 0 is center
CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
// What's the best syntax for this:
// no trailing-closures
// explicit about what's happening, but those ()'s aren't needed... can we do better?
throwActions.filter({ $0.direction == dir }).map({ $0.action() })
// all trailing closures
// weirdly looks like we're mapping the block, rather than the result of filtering throwActions using that block.
@coryalder
coryalder / UIImage+Invert.swift
Created March 12, 2015 05:42
Invert a UIImage at runtime using Core Image CIFilter + CIContext
extension UIImage {
func invertedImage() -> UIImage? {
let img = CoreImage.CIImage(CGImage: self.CGImage)
let filter = CIFilter(name: "CIColorInvert")
filter.setDefaults()
filter.setValue(img, forKey: "inputImage")