This file contains 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
NSError *error = nil; | |
NSData *json = [NSData dataWithContentsOfFile:path]; | |
id object = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableContainers error:&error]; | |
// object is a dictionary or an array you can now use |
This file contains 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
# Here's what it looks like over multiple lines. Use the single line | |
# one below in the console or it won't work. | |
# ["AL","AK","AZ","AR","CA","CO","CT","DE","FL","GA","HI","ID","IL", | |
# "IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT", | |
# "NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI", | |
# "SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY","AS","DC", | |
# "FM","GU","MH","MP","PW","PR","VI", | |
# "AE","AA","AE","AE","AE","AP"].each { |st| group = Group.new( :name => st); group.save } | |
["AL","AK","AZ","AR","CA","CO","CT","DE","FL","GA","HI","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY","AS","DC","FM","GU","MH","MP","PW","PR","VI","AE","AA","AE","AE","AE","AP"].each { |st| group = Group.new( :name => st); group.save } |
This file contains 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
+ (void)writeAnimatedGIFToPath:(NSURL*)path withImages:(NSArray*)images duration:(CGFloat)duration completionBlock:(dispatch_block_t)completionBlock; | |
{ | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | |
NSDictionary *fileProperties = @{ | |
(__bridge id)kCGImagePropertyGIFDictionary: @{ | |
(__bridge id)kCGImagePropertyGIFLoopCount: @0, // 0 means loop forever | |
} | |
}; | |
NSDictionary *frameProperties = @{ |
This file contains 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 NSManagedObject { | |
func safeSetValuesForKeys(dictionary:[NSObject:AnyObject]) { | |
let attributes: [NSObject:AnyObject] = self.entity.attributesByName | |
var finalValue : AnyObject? = nil | |
for (attribute, value) in attributes { | |
if let val : NSNull = value as? NSNull { | |
continue; | |
} |
This file contains 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
func checkShouldDownloadFileAtLocation(urlString:String, completion:((shouldDownload:Bool) -> ())?) { | |
var request = NSMutableURLRequest(URL: NSURL(string: urlString)!) | |
request.HTTPMethod = "HEAD" | |
var session = NSURLSession.sharedSession() | |
var err: NSError? | |
var task = session.dataTaskWithRequest(request, completionHandler: { [weak self] data, response, error -> Void in | |
if let strongSelf = self { | |
var isModified = false |
This file contains 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 String | |
{ | |
var length: Int { | |
get { | |
return self.characters.count | |
} | |
} | |
func contains(s: String) -> Bool { | |
return self.rangeOfString(s) != nil ? true : false |
This file contains 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
// ImagePicker encapsulates UIImagePickerViewController functioality providing a convenient | |
// closure interface for responding to user interactions | |
import UIKit | |
import MobileCoreServices | |
class ImagePicker : NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate { | |
var didFinishPickingMediaWithInfo:((info:[String:AnyObject]) -> ())? | |
var didCancelPickingMedia:(() -> ())? | |
let imagePicker = UIImagePickerController() |
This file contains 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 RangeReplaceableCollectionType where Generator.Element : Equatable { | |
mutating func appendDistinct(object : Generator.Element) { | |
if !self.contains(object) { | |
self.append(object) | |
} | |
} | |
} | |
// A derivative solution using filter instead of contains | |
extension RangeReplaceableCollectionType where Generator.Element : Equatable { |
This file contains 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 Sequence where Iterator.Element == String { | |
var sortedByNumberAndString : [String] { | |
return self.sorted { (s1, s2) -> Bool in | |
return s1.compare(s2, options: .numeric) == .orderedAscending | |
} | |
} | |
} | |
let sorted = ["8 Bob", "7 Joe", "11 Jimmy", "9 Larry", "1 Kyle"].sortedByNumberAndString |
OlderNewer