Skip to content

Instantly share code, notes, and snippets.

@beccadax
beccadax / dispatch-sync-safe.swift
Created June 13, 2014 19:50
In reply to @rnapier, here's how to reduce the implicit optionals in your code: factor them out.
func result() -> A {
var result : A = dispatch_sync(resultQueue, {
return self.result
})
return result
}
func dispatch_sync<R> (queue: dispatch_queue_t, block: Void -> R ) -> R {
var result: R!
dispatch_sync(queue) {
@RoyalIcing
RoyalIcing / example.playground
Created July 13, 2014 09:03
Using enum for getting/setting value and modification date – Inessential · Swift Structs and valueForKey:
import Swift
import Cocoa
// Properties you need as an enum - problem of key value coding is it allows you to type *anything*, typos compile fine.
enum SyncObjectPropertyName {
case Archived
case Title
}
// Protocol shared both for local and server
@ka-interview
ka-interview / gist:a6478004c6cee552f225
Created October 14, 2014 23:57
Sketch of type safe, non-AnyObject-supporting data source
public protocol CountableCollectionType: CollectionType {
var count: Int { get }
}
extension Array: CountableCollectionType {}
public protocol CollectionViewCellFactoryType {
typealias Item
typealias Cell: UICollectionViewCell
func cellForItem(item: Item, inCollectionView collectionView: UICollectionView, atIndexPath indexPath: NSIndexPath) -> Cell
@chriseidhof
chriseidhof / :(
Created November 4, 2014 10:41 — forked from kostiakoval/:(
import Foundation
import ImageIO
infix operator >>= { associativity left }
func >>=<A,B>(l: A?, r: A -> B?) -> B? {
if let x = l {
return r(x)
}
return nil
@JadenGeller
JadenGeller / Swift Coalesce Function.swift
Last active August 29, 2015 14:16
Swift Coalesce Function
/*
* coalesce takes in a list of optional values
* and returns the first one that is not nil
*/
func coalesce<T>(all: @autoclosure () -> T? ...) -> T? {
for f: () -> T? in all {
if let x = f() { return x }
}
return nil
@pdxmph
pdxmph / oo_to_org.rb
Created August 6, 2012 16:55
Simple OmniOutliner outline to org mode
#!/usr/bin/env ruby
require "rubygems"
require "appscript"
include Appscript
oo = app("OmniOutliner Professional")
outline = oo.documents[1].get
@NachoSoto
NachoSoto / AnyValidator.swift
Last active December 3, 2015 14:21
Type-erased ValidatorType
protocol ValidatorType {
typealias ValidatedType
func isValid(object: ValidatedType) -> Bool
}
// You might want to make it a reference type, depending on your case.
struct AnyValidator<T>: ValidatorType {
private let validatorBlock: (T) -> Bool
init<V: ValidatorType where V.ValidatedType == T>(validator: V) {
@frenchy64
frenchy64 / mapcat.clj
Created December 27, 2013 05:47
mapcat
clojure.core/mapcat
(All [c b ...]
[[b ... b -> (Option (Seqable c))] (Option (Seqable b)) ... b -> (Seq c)])
@thinkclay
thinkclay / Keychain.swift
Last active August 30, 2016 12:13
Dead simple keychain access
import Security
public class Keychain
{
public class func set(key: String, value: String) -> Bool
{
if let data = value.dataUsingEncoding(NSUTF8StringEncoding)
{
return set(key, value: data)
}
@chriseidhof
chriseidhof / gcd.swift
Created August 28, 2014 01:08
GCD Wrappers
import Foundation
// Executes an array of blocks in parallel, but only returns after they're all done.
func parallel(blocks: [() -> ()]) {
let group = dispatch_group_create()
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
for block in blocks {
dispatch_group_async(group, queue, block)
}