Skip to content

Instantly share code, notes, and snippets.

- (NSManagedObjectContext *)managedObjectContext {
if (! _managedObjectContext) {
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Moviedo" withExtension:@"momd"];
NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
_managedObjectContext.persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];
NSURL *storeURL = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:@"Moviedo.sqlite"];
NSDictionary *options = @{
@rnystrom
rnystrom / :(
Created November 3, 2014 16:37
A friendly challenge to anyone w/ functional chops to improve this function. No holds barred: op overloading, enum/struct/class, everything-is-a-singleton... go for it! The only rule is no forced unwrapping (e.g. !).
import Foundation
import ImageIO
func datesFromImagesInDir(dir: String) -> [NSDate] {
let fm = NSFileManager.defaultManager()
var error: NSError?
let df = NSDateFormatter()
df.dateFormat = "yyyy:MM:dd HH:mm:ss"
func showScheme(sender: ScalingPageControl) {
performSegueWithIdentifier(SegueIdentifier.PresentSchemeSegue.rawValue/* :( */, sender: pageControl)
}
@rnystrom
rnystrom / SaveSpot.m
Created March 27, 2012 22:22
Implementation file for SaveSpot.m that is having performance issues
#import "SaveSpot.h"
@implementation SaveSpot
@synthesize spot = _spot;
@synthesize maxSpeedLabel = _maxSpeedLabel;
@synthesize avgSpeedLabel = _avgSpeedLabel;
@synthesize distanceLabel = _distanceLabel;
@synthesize nameTextField = _nameLabel;
@synthesize descriptionTextField = _descriptionLabel;
@rnystrom
rnystrom / Iphone Icon.png Bash Script
Created April 5, 2012 15:04 — forked from jessedc/iOS Icon.png bash script
A simple bash script using OSX command line tool sips to resample a 512x512px image
#!/bin/zsh
# Compatible with bash, just change "zsh" to "bash"
# First param: The folder that the original file is stored in
# Second param: The original image name
sips --resampleWidth 512 "${1}/${2}" --out "${1}/iTunesArtwork"
sips --resampleWidth 57 "${1}/${2}" --out "${1}/Icon.png"
sips --resampleWidth 114 "${1}/${2}" --out "${1}/Icon@2x.png"
sips --resampleWidth 29 "${1}/${2}" --out "${1}/Icon-Small.png"
sips --resampleWidth 58 "${1}/${2}" --out "${1}/Icon-Small@2x.png"
@rnystrom
rnystrom / gist:4733270
Created February 7, 2013 19:03
Small category I wrote to simplify displaying a UIImagePickerController, asking where the image should come from, and then running all the stuff. Just setup a UIImagePickerControllerDelegate and forget all of the UIActionSheet business.
// UIImagePickerController+ImageTypeActionSheet.h
@interface UIImagePickerController (ImageTypeActionSheet)
- (void)promptImagePickerTypeWithText:(NSString*)text titles:(NSArray*)titles delegate:(id <UIImagePickerControllerDelegate,UINavigationControllerDelegate>)delegate;
@end
// UIImagePickerController+ImageTypeActionSheet.m
@interface ImageTypeActionSheetDelegate : NSObject
<UIActionSheetDelegate>
@rnystrom
rnystrom / gist:4741259
Last active December 12, 2015 08:08
Testing using JSON data vs an NSObject with properties. Testing initialization of NSObjects and calculations of keyPath values using @AvG. Tested with CodeRunner: http://krillapps.com/coderunner/ Results: http://bit.ly/12zlUNp
#import <Foundation/Foundation.h>
@interface Hospital : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *address;
@property (nonatomic, strong) NSString *city;
@property (nonatomic, strong) NSString *county;
@property (nonatomic, strong) NSString *state;
@property (nonatomic, strong) NSNumber *annualVisitors;
@property (nonatomic, strong) NSString *zip;
@rnystrom
rnystrom / gist:4980453
Created February 18, 2013 20:33
Setting up RNSwipeViewController in code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// PTMasterController is a subclass of RNSwipeViewController
PTMasterController *masterController = [[PTMasterController alloc] init];
PTSchemeController *scheme = [[PTSchemeController alloc] init];
PTUtilityController *utility = [[PTUtilityController alloc] init];
PTWritingController *writing = [[PTWritingController alloc] init];
masterController.centerViewController = writing;
masterController.rightViewController = utility;
@rnystrom
rnystrom / gist:5052675
Created February 27, 2013 23:04
Masking a chip out of a UIView.
- (void)maskChip {
UIGraphicsBeginImageContextWithOptions(self.containerView.bounds.size, NO, [UIScreen mainScreen].scale);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect frame = self.containerView.bounds;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 0, 0);
CGPathAddLineToPoint(path, NULL, frame.size.width / 2.f - 6, 0);
CGPathAddLineToPoint(path, NULL, frame.size.width / 2.f, 6);
CGPathAddLineToPoint(path, NULL, frame.size.width / 2.f + 6, 0);
@rnystrom
rnystrom / gist:5091412
Last active December 14, 2015 13:08
Fast upload of test data to Parse https://parse.com/
#import <Foundation/Foundation.h>
NSMutableURLRequest *parseRequest() {
NSMutableURLRequest *request = [[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://api.parse.com/1/classes/YOUR_CLASS_NAME"]] mutableCopy];
[request setValue:@"YOUR_APPLICATION_ID" forHTTPHeaderField:@"X-Parse-Application-Id"];
[request setValue:@"YOUR_REST_API_KEY" forHTTPHeaderField:@"X-Parse-REST-API-Key"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
return request;