Skip to content

Instantly share code, notes, and snippets.

View quellish's full-sized avatar

Dan quellish

View GitHub Profile
@quellish
quellish / imageMimeTypesArray.m
Last active August 29, 2015 14:06
Method for getting an array of MIME types for images that ImageIO and UIKit can understand.
@import ImageIO;
@import MobileCoreServices;
+ (NSArray *) imageMIMETypes {
NSArray *result = nil;
NSMutableArray *mimeTypes = nil;
NSArray *typeIdentifiers = nil;
typeIdentifiers = (__bridge_transfer NSArray*)CGImageSourceCopyTypeIdentifiers();
mimeTypes = [[NSMutableArray alloc] init];
@quellish
quellish / setDateString.m
Created September 21, 2014 08:52
JSON to Objective-C date accessor
- (void) setDateString:(NSString *)string {
NSDate *date = [someDateThing dateFromISO8601String:string];
return [self setDate:date];
}
@quellish
quellish / coredatajsonremap.m
Created September 21, 2014 08:50
Remapping JSON keys to Core Data attributes using the userInfo dictionary of the attribute entity description as defined in the Core Data Model Editor
entityUserInfo = [entity userInfo];
reKeyedValues = [[NSMutableDictionary alloc] initWithDictionary:jsonObject];
for (NSString *key in keyedValues){
if ([entityUserInfo valueForKey:key] != nil){
[reKeyedValues setValue:[keyedValues valueForKey:key] forKey:[entityUserInfo valueForKey:key]];
// Remove the original key
[reKeyedValues setValue:nil forKey:key];
}
}
@quellish
quellish / pass.m
Created September 21, 2014 08:49
Pass-through acccesor for remapping json key to objective-c property
- (void) setFirst_name:(NSString *)name {
return [self setFirstName:name];
}
@quellish
quellish / JSONCoreData.m
Created September 21, 2014 08:47
Assigning JSON values to a Core Data Managed Object
NSEntityDescription *entityDescription = nil;
NSArray *attributeNames = nil;
NSDictionary *mappedValues = nil;
entityDescription = [managedObject entity];
attributeNames = [[entity attributesByName] allKeys];
mappedValues = [jsonObject dictionaryWithValuesForKeys:attributeKeys];
[managedObject setValuesForKeysWithDictionary:mappedValues];
@quellish
quellish / JSONPersonDataSource.m
Created September 21, 2014 08:45
JSONPersonDataSource
NSString * const JSONPersonFirstNameKeyPath = @"firstName";
NSString * const JSONPersonLastNameKeyPath = @"lastName";
@implementation JSONPersonDataSource
- (instancetype) initWithJSON:(id)json {
if ((self = [super init])){
jsonObject = json;
}
return self;
@quellish
quellish / PersonDataSource.m
Created September 21, 2014 08:43
PersonDataSource
@protocol PersonDataSource
- (NSString *)firstName;
- (NSString *)lastName;
@end
@quellish
quellish / jsonparse.m
Created September 21, 2014 08:42
Parsing JSON correctly
NSError *error = nil;
id jsonObject = nil;
jsonObject = [NSJSONSerialization JSONObjectWithData:data options:options:NSJSONReadingAllowFragments error:&error];
if (jsonObject != nil){
[self dostuffWith:jsonObject];
} else {
[self presentError:error];
}
@quellish
quellish / setvalue.m
Created September 21, 2014 08:39
Empty setValue:forUndefinedKey:
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
return;
}
@quellish
quellish / connectionerrror.m
Created September 20, 2014 19:22
Example of correct checking of connection errors in objective-c
[NSURLConnection sendAsynchronousRequest:request queue:[self connectionQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (response != nil){
/// Yay, we were able to connect!
} else {
[self presentError:connectionError];
}
}];