Validate email address on the iPhone

Here is some simple code that allows you to validate email addresses that have been entered by a user on an iPhone. It does use NSPredicate, which is only available on OS 3.0 and above;

	NSString *email = [newEmailAddress.text lowercaseString];
	NSString *emailRegEx =
	NSPredicate *regExPredicate =[NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];
	BOOL myStringMatchesRegEx = [regExPredicate evaluateWithObject:email];
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Correct Email" message:@"Please enter a valid email address" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[alert show];
		[alert release];

The code is based on this article which describes this in a lot more detail.

Core Data – Updated model

After creating a new version of your data model, and adding the relevant code to handle lightweight migration, to actually get your code to run ensure that you do a clean all targets otherwise you get merge model errors!

Core Data – fetch a single entity

I’ve used the following code to retrieve an entity from Core Data, so that the result returns the entity object rather than a Dictionary

-(Target *) currentTargetInContext:(NSManagedObjectContext *)context {
	Target *match;
	NSFetchRequest *request = [[NSFetchRequest alloc] init];
	NSEntityDescription *entity = [NSEntityDescription entityForName: @"Target" inManagedObjectContext:context];
	[request setEntity:entity];
	[request setResultType:NSManagedObjectResultType];
	NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Achieved == 0 "];
	[request setPredicate:predicate];
	NSError *error;
	NSArray *objects = [context executeFetchRequest:request error:&error];
	match = nil;
	if (objects == nil)
		// handle the error
		if ([objects count] > 0)
			match = (Target *)[objects objectAtIndex:0];
	[request release];
	return match;

CoreData – SQLite Manager for Firefox

This Firefox add-on allows you to view the data you are running against while using the iPhone simulator. Your sqlite files can be found in the following folder

/Users/”user name”/Library/Application Support/iPhone Simulator/User/Applications/”application guid”/Documents

SQLite Firefox add-on

This is a great tool for view the data you’ve created within your apps, but I would suggest that is best to define your data model via Xcode

Data Model change causes iPhone Simulator to fail

When you alter your underlying data model the iPhone simulator will not be able to load your app as the cached version of the database doesn’t match the new one. The easiest way to resolve the problem is to run the simulator, click and hold your app and remove it like you would remove an app from your actual phone.

Create Objective-C classes from Core Data model

Once you’ve created your data model within Xcode, you may need to generate the classes so that you can use and reference them in your main project. To do this open the data model, select File/New File… from the Xcode menu system, this will display the following screen…

New File screen with Managed Object Class selected

New File screen with Managed Object Class selected

Choose your location and then select the entities you want created.