This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| import QuartzCore | |
| class ViewController: UIViewController { | |
| @IBOutlet weak var label: UILabel | |
| @IBOutlet weak var counter: UILabel | |
| override func viewDidLoad() { | |
| super.viewDidLoad() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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": [ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Swift | |
| /*: | |
| A simple type-erased sequence | |
| */ | |
| let seq = AnySequence([1,2,3]) | |
| /*: | |
| ## Who Needs Types Like That? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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()) } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //: 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) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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. | |
| /// |