简体   繁体   中英

How to store table value into core data entity in IOS

I am having data Like This:

Appliance        Average Power Rating (Watts)   Alternate  Power Rating (Watts)
Fridge/Refrigerator       120                                       55 
Microwave                 1000                                     1000 
Mixie                     700                                       200 
Roti Maker                30                                        300

I want to store this data into my entity in CoreData. Just I have created an entity called EY_Appliance with attributes applianceId , applianceName , watts and amountPerWatts .

You have to write code in this way. This will insert new Object for your entity with following attributes. You can edit it as per your requirement.

NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *powerDesc = [NSEntityDescription insertNewObjectForEntityForName:@"EY_Appliance" inManagedObjectContext:context];


[powerDesc setValue:self.applianceId.text forKey:@"applianceId"];
[powerDesc setValue:self.applianceName.text forKey:@"applianceName"];
[powerDesc setValue:self.watts.text forKey:@"watts"];
[powerDesc setValue:self.amountPerWatts.text forKey:@"amountPerWatts"];

// Save the context
NSError *error = nil;
if (![context save:&error]) {
    NSLog(@"Saving Failed!, Error and its Desc %@ %@", error, [error localizedDescription]);
}

For that you can create and add the core data object as follows

NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *object = [NSEntityDescription insertNewObjectForEntityForName:@"EY_Appliance" inManagedObjectContext:context];
[object setValue:self.applianceIdValue forKey:@"applianceId"];
[object setValue:self.applianceNameValue forKey:@"applianceName"];
[object setValue:self.wattsValue forKey:@"watts"];
[object setValue:self.amountPerWattsValue forKey:@"amountPerWatts"];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Saving Failed with error %@", error);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM