Skip to content

Instantly share code, notes, and snippets.

class Horse {
var name:String?
var beenThroughDesert:Bool = false
init(){}
init(horsename:String?, desert:Bool) {
if let n = horsename {
name = n
}
beenThroughDesert = desert
}
@SteveTrewick
SteveTrewick / ListNode.swift
Created June 19, 2014 12:48
Workaround for LLVM error when using generic class type parameters in Swift
class ListNode<T> {
var items:T[] = []
var item: T { get { return items[0] } set { items = [newValue] }}
var nextNode:ListNode?
init( item i:T ) { item = i}
}
@SteveTrewick
SteveTrewick / recursive-completion-backoff.m
Last active August 29, 2015 14:03
Recursive completion handlers with retry and back off using some higher order functions via blocks. Not sure about this at all, but it was a way to spend an afternoon, eh ?
#import <Foundation/Foundation.h>
typedef void(^CompletionHandler)(id connection, id result, NSError * error);
typedef BOOL(^Counter)();
typedef void(^RequestWrapper)(CompletionHandler);
@interface SMRequestFoo : NSObject
+(void)
request :(id ) thing
completion:(CompletionHandler) completion;
@SteveTrewick
SteveTrewick / gist:f2f67bc2e40b86f40df9
Last active August 29, 2015 14:03
A cleaner version of the previous code without some of the __weak, __strong line noise. Recursive completion blocks in ObjC using some higher order functions.
#import <Foundation/Foundation.h>
typedef void(^CompletionHandler)(id connection, id result, NSError * error);
typedef BOOL(^Counter)();
typedef void(^RequestWrapperComp)();
@interface SMRequestFoo : NSObject
+(void)
request :(id ) thing
[_photo_library
assetForURL: url
resultBlock:^(ALAsset *asset)
{
ALAssetRepresentation * rep = [asset defaultRepresentation];
NSDictionary * meta = [rep metadata];
if( meta[ @"AdjustmentXMP" ] ) {
// Image is filtered, add to list
}
@SteveTrewick
SteveTrewick / RFC822DateParser.swift
Last active August 29, 2015 14:24
Parse an RFC 822 date (e.g. for RSS/Atom feeds) based on some easy to spot characteristics of the format.
// Because [EEE, ]dd MMM yyyy HH:mm[:ss] z
// we can split based on , and : and count.
// There is probably a better way to do this
// without splitting the strings.
func dateFromPubdateString(str:String) -> NSDate? {
func hasDay(str:String) -> Bool {
return str.componentsSeparatedByString(",").count > 1
}
@SteveTrewick
SteveTrewick / NSColorPanel.swift
Created September 24, 2015 14:09
Using NSColorPanerl with Swift on OS X
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
func applicationDidFinishLaunching(aNotification: NSNotification) {
let cp = NSColorPanel.sharedColorPanel()
@SteveTrewick
SteveTrewick / bgTask.swift
Created October 16, 2015 14:07
Swift beginBackgroundTaskWithExpirationHandler snippet
var bgTask:UIBackgroundTaskIdentifier!
bgTask = app.beginBackgroundTaskWithExpirationHandler { () -> Void in
self.app.endBackgroundTask(bgTask)
}
// Don't do this
@SteveTrewick
SteveTrewick / bglocation.swift
Last active October 16, 2015 15:15
Swift ios9 background core location
/*
In your .plist :
<key>UIBackgroundModes</key>
<array>
<string>location</string>
</array>
...
<key>UIRequiredDeviceCapabilities</key>
<array>
...
@SteveTrewick
SteveTrewick / duckspeak.swift
Last active February 20, 2017 11:06
Set AVAudioSession category and options to mix with other audio and turn volume down/up as well as interrupt any spoken text.
import UIKit
import AVFoundation
class ViewController: UIViewController, AVSpeechSynthesizerDelegate {
let synth = AVSpeechSynthesizer()
let avsesh = AVAudioSession.sharedInstance()
let voice = AVSpeechSynthesisVoice(language: "en-GB")
let avopts:AVAudioSessionCategoryOptions = [
.MixWithOthers,
.DuckOthers,