What’s the optimum way of storing an NSDate in NSUserDefaults?

You are needlessly complicating things. Why are you converting the date to a time interval (then the time interval to a different primitive)? Just [sharedDefaults setObject:theDate forKey:@”theDateKey”] and be done with it. NSDate is one of the “main types” supported by the PLIST format (dates, numbers, strings, data, dictionaries, and arrays), so you can just … Read more

NULL vs nil in Objective-C

nil should only be used in place of an id, what we Java and C++ programmers would think of as a pointer to an object. Use NULL for non-object pointers. Look at the declaration of that method: – (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context Context is a void * (ie a C-style pointer), so … Read more

What’s the Best Way to Shuffle an NSMutableArray?

I solved this by adding a category to NSMutableArray. Edit: Removed unnecessary method thanks to answer by Ladd. Edit: Changed (arc4random() % nElements) to arc4random_uniform(nElements) thanks to answer by Gregory Goltsov and comments by miho and blahdiblah Edit: Loop improvement, thanks to comment by Ron Edit: Added check that array is not empty, thanks to … Read more

Best way to remove from NSMutableArray while iterating?

For clarity I like to make an initial loop where I collect the items to delete. Then I delete them. Here’s a sample using Objective-C 2.0 syntax: NSMutableArray *discardedItems = [NSMutableArray array]; for (SomeObjectClass *item in originalArrayOfItems) { if ([item shouldBeDiscarded]) [discardedItems addObject:item]; } [originalArrayOfItems removeObjectsInArray:discardedItems]; Then there is no question about whether indices are … Read more

Check that an email address is valid on iOS [duplicate]

Good cocoa function: -(BOOL) NSStringIsValidEmail:(NSString *)checkString { BOOL stricterFilter = NO; // Discussion http://blog.logichigh.com/2010/09/02/validating-an-e-mail-address/ NSString *stricterFilterString = @”^[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}$”; NSString *laxString = @”^.+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2}[A-Za-z]*$”; NSString *emailRegex = stricterFilter ? stricterFilterString : laxString; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@”SELF MATCHES %@”, emailRegex]; return [emailTest evaluateWithObject:checkString]; } Discussion on Lax vs. Strict – http://blog.logichigh.com/2010/09/02/validating-an-e-mail-address/ And because categories are just better, … Read more

How to get pixel data from a UIImage (Cocoa Touch) or CGImage (Core Graphics)?

FYI, I combined Keremk’s answer with my original outline, cleaned-up the typos, generalized it to return an array of colors and got the whole thing to compile. Here is the result: + (NSArray*)getRGBAsFromImage:(UIImage*)image atX:(int)x andY:(int)y count:(int)count { NSMutableArray *result = [NSMutableArray arrayWithCapacity:count]; // First get the image into your data buffer CGImageRef imageRef = [image … Read more

Xcode build failure “Undefined symbols for architecture x86_64”

It looks like you are missing including the IOBluetooth.framework in your project. You can add it by: Clicking on your project in the upper left of the left pane (the blue icon). In the middle pane, click on the Build Phases tab. Under “Link Binary With Libraries”, click on the plus button. Find the IOBluetooth.framework … Read more

iOS – Build fails with CocoaPods cannot find header files

Update Make sure your Podfile includes link_with on targets missing a config file. Cocoapods only sets the first target by default otherwise. e.g. platform :osx, ‘10.7’ pod ‘JSONKit’, ‘~> 1.4’ link_with ‘Pomo’, ‘Pomo Dev’, ‘Pomo Tests’ —— End Update Note: Please do note that you have to look into Project->Info->Configurations for steps below. I had … Read more

Execute a terminal command from a Cocoa app

You can use NSTask. Here’s an example that would run ‘/usr/bin/grep foo bar.txt‘. int pid = [[NSProcessInfo processInfo] processIdentifier]; NSPipe *pipe = [NSPipe pipe]; NSFileHandle *file = pipe.fileHandleForReading; NSTask *task = [[NSTask alloc] init]; task.launchPath = @”/usr/bin/grep”; task.arguments = @[@”foo”, @”bar.txt”]; task.standardOutput = pipe; [task launch]; NSData *data = [file readDataToEndOfFile]; [file closeFile]; NSString *grepOutput … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)