Friday, March 12, 2010

Foundation Code: CoreDataControl

The following code set is used as the start of a core data wrapper. Instead of having the same core data code in every class, use it one place.  This is the start, but certainly needs more work.  As the refactoring effort continues, code will be migrated into this class.

This is pretty standard stuff, any tutorial on core data has it, the only tricks are that I pass in NSStrings for table names, and that I don't put it in my application delegate.  The source for this can be found @

Original header

Original Source



Extending set to get a sorted array


// An extension of NSSet to get a sorted array from that set.

@interface NSSet(ArrayMethods)
- (NSArray*) getSortedArray:(NSString*) primaryKey;
@end




@implementation NSSet(ArrayMethods)
- (NSArray*) getSortedArray:(NSString*) primaryKey
{
NSSortDescriptor *sortDesc =
[[NSSortDescriptor alloc] initWithKey:primaryKey
ascending:YES
  selector:@selector(localizedCaseInsensitiveCompare:) ];
NSArray *descArray=[[NSArray alloc] initWithObjects:sortDesc,nil] ;
NSArray *returnValue=[[self allObjects] sortedArrayUsingDescriptors:descArray] ;
[descArray release];
[sortDesc release];

return returnValue ;
}
@end


No comments:

Post a Comment