Thursday, June 28, 2012

Save NSDate in CoreData

1. In your model file.
Create an entity MyDate with attribute datestring of datatype date(select it from dropdown).

2. Save your date

    //Create date from the string
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    NSDate *date=[dateFormatter dateFromString:dateString_];
    [dateFormatter release];
    //Save the auditDate Now    
    MyDate *obj = [NSEntityDescription insertNewObjectForEntityForName:@"MyDate" inManagedObjectContext:context];
    [obj setValue:date forKey:@"datestring"];
    NSError* error = nil;
    [context save:&error];
    if (error)
        NSLog(@"%@", error);

3. Retrieve your date

When you  retrieve  MyDate objects, accessing value of key datestring will return NSDate, you can convert that to NSString to display it on your screen.

No comments:

Post a Comment