Storing custom objects in an NSMutableArray in NSUserDefaults

For loading custom objects in an array, this is what I’ve used to grab the array: NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults]; NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:@”savedArray”]; if (dataRepresentingSavedArray != nil) { NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray]; if (oldSavedArray != nil) objectArray = [[NSMutableArray alloc] initWithArray:oldSavedArray]; else objectArray = [[NSMutableArray alloc] init]; } You should check … Read more

Delete/Reset all entries in Core Data?

You can still delete the file programmatically, using the NSFileManager:removeItemAtPath:: method. NSPersistentStore *store = …; NSError *error; NSURL *storeURL = store.URL; NSPersistentStoreCoordinator *storeCoordinator = …; [storeCoordinator removePersistentStore:store error:&error]; [[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error]; Then, just add the persistent store back to ensure it is recreated properly. The programmatic way for iterating through each entity is both … Read more

How can I create a UIColor from a hex string?

I’ve found the simplest way to do this is with a macro. Just include it in your header and it’s available throughout your project. #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0] uicolor macro with hex values Also formatted version of this code: #define UIColorFromRGB(rgbValue) \ … Read more

What’s the best way to communicate between view controllers?

These are good questions, and its great to see that you’re doing this research and seem concerned with learning how to “do it right” instead of just hacking it together. First, I agree with the previous answers which focus on the importance of putting data in model objects when appropriate (per the MVC design pattern). … Read more

Get to UIViewController from UIView?

Using the example posted by Brock, I modified it so that it is a category of UIView instead UIViewController and made it recursive so that any subview can (hopefully) find the parent UIViewController. @interface UIView (FindUIViewController) – (UIViewController *) firstAvailableUIViewController; @end @implementation UIView (FindUIViewController) – (UIViewController *) firstAvailableUIViewController { UIResponder *responder = [self nextResponder]; while … Read more

Placeholder in UITextView

I made a few minor modifications to bcd’s solution to allow for initialization from a Xib file, text wrapping, and to maintain background color. Hopefully it will save others the trouble. UIPlaceHolderTextView.h: #import <Foundation/Foundation.h> IB_DESIGNABLE @interface UIPlaceHolderTextView : UITextView @property (nonatomic, retain) IBInspectable NSString *placeholder; @property (nonatomic, retain) IBInspectable UIColor *placeholderColor; -(void)textChanged:(NSNotification*)notification; @end UIPlaceHolderTextView.m: #import … Read more

How can I upload a photo to a server with the iPhone?

Header: @interface EPUploader : NSObject { NSURL *serverURL; NSString *filePath; id delegate; SEL doneSelector; SEL errorSelector; BOOL uploadDidSucceed; } – (id)initWithURL: (NSURL *)serverURL filePath: (NSString *)filePath delegate: (id)delegate doneSelector: (SEL)doneSelector errorSelector: (SEL)errorSelector; – (NSString *)filePath; @end Main: #import “EPUploader.h” #import <zlib.h> static NSString * const BOUNDRY = @”0xKhTmLbOuNdArY”; static NSString * const FORM_FLE_INPUT = @”uploaded”; … Read more

How can we programmatically detect which iOS version is device running on? [duplicate]

Best current version, without need to deal with numeric search within NSString is to define macros (See original answer: Check iPhone iOS Version) Those macros do exist in github, see: https://github.com/carlj/CJAMacros/blob/master/CJAMacros/CJAMacros.h Like this: #define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame) #define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending) #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] … Read more