XCode 4 Color Theme

Here is my  XCode 4 Color Theme. I mostly use it during my nightly cocoa programming sessions. I love dark backgrounds because I find them particularly soothing to the eye. This theme is based on the website color palette.

To install the theme unzip the file into your /Users/yourname/Library/Developer/Xcode/UserData/FontAndColorThemes folder.

ObjectiveSheep XCode Color Theme

Works especially well if combined with some good Jazz through your headphones… something like Miles Davis’s “Kind of Blue” for example :-) . Enjoy.

Posted in Uncategorized | Leave a comment

NSImage (or UIImage) into Core Data

A problem I have encountered and that took me a lot of searching on google is how to store an NSImage into a core data field.
The solution is actually a very simple one but without guide it could be awfully time consuming.

So here it is.

First of all you need a subclass of NSValueTransformer to do the job:

Here is the interface:

@interface SheepImageToDataTransformer : NSValueTransformer



And the implementation:

#import "SheepImageToDataTransformer.h"

@implementation SheepImageToDataTransformer

+ (BOOL)allowsReverseTransformation


return YES;


+ (Class)transformedValueClass


return [NSData class];


// These two methods use the NSImage implementation that is Mac specific or the iOS one with UIImage. Choose the lines that apply to your case and comment/delete the others.
- (id)transformedValue:(id)value


// Mac implementation 
NSBitmapImageRep *rep = [[value representations] objectAtIndex: 0];
NSData *data = [rep representationUsingType: NSPNGFileType
properties: nil];
return data;

 // -- OR --

// iOS implementation  
return UIImagePNGRepresentation(value); 


- (id)reverseTransformedValue:(id)value


// Mac implementation: 
NSImage *uiImage = [[NSImage alloc] initWithData:value];
return uiImage;

// -- OR -- 

 // iOS implementation:
return [UIImage imageWithData:value];



Just import the header into your NSManagedObject subclass and declare your property as an NSImage like this:

@property (nonatomic, retain) NSImage * thumbnail;

To finish things up you will be setting the attribute type of the desired field in your data model to Transformable and put the name of your NSValueTransformer subclass in the name field like this:

Core Data

bookmark.thumbnail = yourNSImage;

And you’re all set.

Remember to watch your performance. Don’t store images larger than a 100k as a field of an entity. If you need bigger images store them on disk and save the path to core data.


Posted in Uncategorized | Leave a comment

Core Data + Active Record = SheepData

Let me first of all say that I love Core Data. It’s really powerful and easy to use and coming from java and hibernate I immediately felt comfortable with it.

There are a couple of things that I don’t like though. First of all the logic of having access to your core data objects through the Application Delegate (which is what usually happens if you let XCode create a Core Data project for you) and the repetitive code for simply creating and executing requests.

I have always been a strong believer in Martin Fowler’s Active Record pattern. A single entity should have the responsibility of all its CRUD operations. So… scratching my own itch I ended up with a couple of classes that make life a lot easier when using Core Data out of the box:

No core data code in the Application Delegate. In fact there will hardly be any managed object context and persistent store to be seen anywhere. Plus it can be used on existing projects that have not been created as core data projects before (both iOS and Cocoa)

Simple entity management: all entities are real classes subclassing SheepEntity (offspring of an NSManagedObject) and can be used wherever in your code.


Simple methods for fetching / adding / deleting / checking entities from Core Data

Let’s see this step by step.

1. Download the SheepData example project containing the SheepData folder you will need in your own project from github

2. There will be an XCode 4 project that can be tested (command+u) out of the box. A core data model has been created with a couple of entities.

3. If you open the core data model you will notice that every entity has its own class specified… this is actually very important to make things work so make sure this is filled in.




4. Now select the entity you need and from the menu select Editor and then Create NSManagedObject subclass. You will end up with an entity you can use in your code.




5. Now in your code you will have a Mother.h and Mother.m file. First add the SheepData folder into your XCode project.

@interface Child :  NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSManagedObject * mother;
@property (nonatomic, retain) NSManagedObject * father;


And then add the #import “SheepEntity.h” line in your Mother.h.. also make it extend SheepEntity. Like this:

#import "SheepEntity.h"

@interface Child :  SheepEntity

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSManagedObject * mother;
@property (nonatomic, retain) NSManagedObject * father;


That is all you need! You’re done. Here are a couple of examples that show how easy it is to use a SheepEntity in your code.

// a. Creating a new Entity
// No other code is required. No need to create a managed context and persistent store. Everything is automagically done behind the scenes.

Mother *mother = [[Mother alloc] initEntity];

// b. Fetching an array of entities
NSArray* fathers = [Father fetchEntities];

// c. Fetching a specific entity
Child *c = [Child fetchEntityWhereProperty:@"name" equalsValue:@"Johnny"];

// d. Deleting a set of entities
NSArray* children = [Child fetchEntities];
[Child deleteEntities:children];

You will have access to other useful methods like:

+ (id) fetchEntityWhereProperty:(NSString *)aProperty equalsValue:(id)aValue; //Convenience method for fetching a single entity by property/value

+ (id) fetchEntityWithPredicate:(NSPredicate*) aPredicate; //fetches a single entity with a given predicate

+ (NSArray*) fetchEntities; //fetches all entities

+ (NSArray*) fetchEntitiesWithSortDescriptor:(NSSortDescriptor*)sortDescriptor; //fetches all entities in a given order

+ (NSArray*) fetchEntitiesWhereProperty:(NSString *)aProperty equalsValue:(id) aValue;//Convenience method for fetching a list of entities by property/value

+ (NSArray*) fetchEntitiesWithPredicate:(NSPredicate*) aPredicate; //Convenience method for fetching a list of entities given a predicate

+ (NSArray*) fetchEntitiesWithPredicate:(NSPredicate*) aPredicate andSortDescriptor:(NSSortDescriptor*) aSortDescriptor; //Convenience method for fetching a list of entities given a predicate and a sort descriptor

+ (NSArray*) fetchEntitiesWithPredicate:(NSPredicate*) aPredicate andSortDescriptors:(NSArray*) sortDescriptors; //Convenience method for fetching a list of entities given a predicate and multiple sort descriptors

- (void) deleteEntity; //removes the current entity

+ (void) deleteEntities: (NSArray*) entities; //removes an array of entities in one shot

+ (BOOL) checkIfEntityExistsWhereProperty:(NSString *)aProperty equalsValue:(id) aValue;//checks whether an entity with a given property/value already exists.

+ (BOOL) checkIfEntityExistsWithPredicate:(NSPredicate*) aPredicate; //checks whether an entity with a given predicate already exists.

Feel free to use this code in your projects (commercial or not). It has a BSD license. Attribution is welcome. Enjoy!

Posted in Uncategorized | Leave a comment