Re: Storing structured data in file - any easy way?
Re: Storing structured data in file - any easy way?
- Subject: Re: Storing structured data in file - any easy way?
- From: Jules Colding <email@hidden>
- Date: Sat, 12 Jul 2008 17:25:46 +0200
Hi,
On 12/07/2008, at 02.22, Ben Trumbull wrote:
At 12:39 AM -0700 7/11/08, email@hidden wrote:
> Out of curiosity, what do you find not simple about doing the above
using Core Data?
It is easy enough to create the data model using xcode, but how
that connects to actual code is harder to see.
Maybe I'm just overwhelmed... I've had to learn about Sync
Services, Cocoa, xcode, the Foundation framework and not to forget
Objective-C, from scratch in just the last month or so. I'm new to
the entire Mac development environment (I'm coming from linux) so
you can imagine that my head is rather busy digesting it all.
Anyway, I must say that I find it the best thought-out development
environment that I've ever had the pleasure to work within.
Once you've gotten the Cocoa memory management, and Key Value Coding
concepts down, it's straight forward. The low level Core Data
tutorial is pretty good for showing how the pieces fit together with
the code.
<http://developer.apple.com/documentation/Cocoa/Conceptual/CoreDataUtilityTutorial/00_Introduction/chapter_1_section_1.html
>
Additionally, here's a cmd line example that assumes you've created
a model named 'WordModel' with an entity 'Word' that has an
attribute named 'text'. Of course, it would also require you save
some data for it to be useful. But it shows how to set up a stack
and fetch data.
Thanks a lot Ben! This sample puts code to the bones so to speak ;-)
Best regards,
jules
#import <CoreData/CoreData.h>
#include <stdlib.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSError *error = nil;
// Create a Core Data stack
NSManagedObjectModel* model = [[NSManagedObjectModel alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:@"WordModel.mom"]];
NSPersistentStoreCoordinator *coordinator =
[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:
model];
[model release];
NSManagedObjectContext* context = [[NSManagedObjectContext alloc]
init];
[context setPersistentStoreCoordinator: coordinator];
[context setUndoManager:nil];
[coordinator release];
NSURL *dburl = [NSURL fileURLWithPath: [NSString
stringWithUTF8String:argv[argc-1]]];
NSDictionary* opts = nil;
id store;
if (nil == (store = [coordinator addPersistentStoreWithType:
NSSQLiteStoreType configuration: nil URL: dburl options: opts error:
&error])) {
NSLog(@"Could not access database file. NSError = %@ ui =
%@", error, [error userInfo]);
exit(-1);
}
// Create a fetch request
NSFetchRequest* request = [[[NSFetchRequest alloc] init]
autorelease];
[request setEntity:[NSEntityDescription entityForName:@"Word"
inManagedObjectContext:context]];
// Perform the fetch
NSArray* results = [context executeFetchRequest:request
error:&error];
if (nil == results) {
NSLog(@"Error during fetch = %@ with userInfo = %@", error,
[error userInfo]);
exit(-1);
}
unsigned long count = [results count];
NSString* text;
for (NSManagedObject* word in results) {
text = word.text;
}
[context release];
[pool drain];
return 0;
}
--
-Ben
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden