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
| // | |
| // Set | |
| // | |
| struct MySet<KeyType : Hashable> : Sequence | |
| { | |
| var dictionaryOfItems = Dictionary<KeyType,Bool>() | |
| init() {} | |
| init(array:Array<KeyType>) { |
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
| class Bag<T: Hashable>: Sequence, Printable { | |
| var _storage = Dictionary<T, Int>() | |
| typealias GeneratorType = Dictionary<T,Int>.GeneratorType | |
| func addItem(item: T) { | |
| if let count = _storage[item] { | |
| _storage[item] = count + 1 | |
| } else { | |
| _storage[item] = 1 | |
| } |
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
| class Box<T> { | |
| let unbox:T | |
| init(unbox:T) { self.unbox = unbox } | |
| } | |
| /* | |
| Boxing is necessary because Swift does not allow recursive enum types. | |
| However, AFAICT, is it also desirable here because it enables structural |
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
| /* | |
| The examples below show two ways to model a state machine with two states, | |
| On and Off, with an additional constant Int property. | |
| On -> Off and Off -> On transitions must be inititated by calling flip() | |
| */ | |
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
| extension UIColor | |
| { | |
| convenience init(RGBValue:UInt32) | |
| { | |
| let red = (RGBValue & 0xFF0000) >> 16 | |
| let green = (RGBValue & 0x00FF00) >> 8 | |
| let blue = (RGBValue & 0x0000FF) >> 0 | |
| self.init(red:CGFloat(red)/255.0,green:CGFloat(green)/255.0,blue:CGFloat(blue)/255.0,alpha:CGFloat(1.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
| /// Not sure why this generic version does not work. | |
| private class WrapperTableViewCell<T:UIView> : UITableViewCell | |
| { | |
| class var classReuseIdentifier:String { return T.self.description() + "CellIdentifier" } | |
| override class func requiresConstraintBasedLayout() -> Bool { return true } | |
| override init(style: UITableViewCellStyle, reuseIdentifier: String?) | |
| { | |
| super.init(style: UITableViewCellStyle.Default, reuseIdentifier: reuseIdentifier) |
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
| // | |
| // NSString+RelativePaths.m | |
| #import "NSString+RelativePaths.h" | |
| /** | |
| Returns a path to resourcePath, relative to anchorPath. | |
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
| // | |
| // MARK: generic networking and JSON-decoding helper | |
| // | |
| // give it a NSURLRequest and a session, it gives you back a aresult with an AnyObject or an NSError | |
| public class JSONService | |
| { | |
| /// returns JSON result on statusCode 200, and an error otherwise | |
| public class func taskWithJSONRequest( |
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
| private extension NSURLComponents | |
| { | |
| func withQueryItem(name:String,intValue:Int) -> NSURLComponents { | |
| return self.withQueryItem(name, stringValue: NSNumber(integer: intValue).stringValue) | |
| } | |
| func withQueryItem(name:String,floatValue:Float) -> NSURLComponents { | |
| return self.withQueryItem(name, stringValue: NSNumber(float: floatValue).stringValue) | |
| } | |
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
| /// Save image in an in-memory URL | |
| func toURL(image:UIImage) -> NSURL | |
| { | |
| let img = image | |
| let imgData:NSData = UIImagePNGRepresentation(img) | |
| let dataFormatString = "data:image/png;base64,%@" | |
| let dataString = String(format: dataFormatString, imgData.base64EncodedStringWithOptions(.allZeros)) | |
| let dataURL = NSURL(string: dataString)! | |
| return dataURL | |
| } |
OlderNewer