Skip to content

Instantly share code, notes, and snippets.

@hsavit1
hsavit1 / ViewController.swift
Created November 30, 2015 05:53 — forked from Inferis/ViewController.swift
Beginnings of a GCD wrapper in swift
import UIKit
import QuartzCore
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel
@IBOutlet weak var counter: UILabel
override func viewDidLoad() {
super.viewDidLoad()
import Cocoa
// for-in
func checkForIn(array: [Int], dict: [Int: String]) {
for num in array where dict[num] != nil {
num
}
}
checkForIn([1,2,3,4], dict: [1:"one", 2:"two"])
@hsavit1
hsavit1 / parsing.swift
Created December 3, 2015 03:14 — forked from chriseidhof/parsing.swift
JSON Parsing in Swift
// This code accompanies a blog post: http://chris.eidhof.nl/posts/json-parsing-in-swift.html
//
// As of Beta5, the >>= operator is already defined, so I changed it to >>>=
import Foundation
let parsedJSON : [String:AnyObject] = [
"stat": "ok",
"blogs": [
@hsavit1
hsavit1 / higher-order-funcs.swift
Last active December 12, 2015 00:00 — forked from ijoshsmith/higher-order-funcs.swift
Higher-order functions in Swift
// main.swift
// HigherOrderFunctions
//
// Created by Joshua Smith on 12/6/15.
// Copyright © 2015 iJoshSmith. All rights reserved.
//
/*
This file contains simple implementations of several
higher-order functions in the Swift standard library.
@hsavit1
hsavit1 / erasure.swift
Created December 11, 2015 03:55 — forked from rnapier/erasure.swift
A Little Respect for AnySequence (http://robnapier.net/erasure)
import Swift
/*:
A simple type-erased sequence
*/
let seq = AnySequence([1,2,3])
/*:
## Who Needs Types Like That?
@hsavit1
hsavit1 / 1-simplejson.swift
Created December 11, 2015 03:56 — forked from rnapier/1-simplejson.swift
Yeah, @krzyzanowskim is probably right. What problem were we solving?
import Foundation
// All the complexity needed to avoid this code probably isn't worth it in the majority of cases.
// Particularly note that the data model doesn't 100% line up with the JSON (some keys have
// slightly different names for instance). A system flexible enough to handle that (say, something
// like Go's json.Marshal) would be nice, but this code, while slightly tedious, isn't really bad.
// Basically I'm saying that a richer JSON<->Swift system built into stdlib would be nice, but the
// bar is pretty high to go pulling in a helper library.
//
// That said, compare the Go code below, and I think it really is much simpler, and scales much better
// This should all be on the MOC, not the class.
// A singleton MOC like "CoreDataStack" completely blows up CoreData concurrency.
// You need a separate MOC per queue. So you have to work with the right MOC, not a global one.
// (I'm assuming here that CoreDataStack returns a singleton; maybe it makes a new one magically;
// either way, you should work through the moc so that you have a consistent view of the store.)
// Built on the MOC, this is easy, and matches stuff we've done in ObjC for years.
extension NSManagedObjectContext {
func newInstanceOf<T: NSManagedObject>(type: T.Type) -> T {
return NSEntityDescription.insertNewObjectForEntityForName(NSStringFromClass(T), inManagedObjectContext: self) as! T
}
@hsavit1
hsavit1 / anyseq.swift
Created December 11, 2015 03:57 — forked from rnapier/anyseq.swift
How does Apple do AnySequence?
// Reimplementing AnySequence with a closure is pretty easy,
// and I believe this technique can be applied to any type eraser:
struct MyClosureAnySequence<Element> : SequenceType {
let generator: () -> AnyGenerator<Element>
init<S : SequenceType where S.Generator.Element == Element>(_ base: S) {
generator = { anyGenerator(base.generate()) }
}
init<G : GeneratorType where G.Element == Element>(_ makeUnderlyingGenerator: () -> G) {
generator = { anyGenerator(makeUnderlyingGenerator()) }
//: Convenience functions/extension on top of GCD.
import Dispatch
var MainQueue: dispatch_queue_t { return dispatch_get_main_queue() }
func GlobalQueue(qos: dispatch_qos_class_t = .Default) -> dispatch_queue_t
{
return dispatch_get_global_queue(qos, 0)
}
@hsavit1
hsavit1 / ConstraintCollection.swift
Created December 11, 2015 05:11 — forked from jtbandes/ConstraintCollection.swift
Autolayout constraint literals in Swift
#if os(iOS)
import UIKit
#else
import AppKit
#endif
/// A set of constraints prepared from a visual format string, in the style of
/// `NSLayoutConstraint.constraintsWithVisualFormat()`, with the additional ability
/// to supply views and metrics in a string interpolation.
///