我有这样的数据:
Appliance Average Power Rating (Watts) Alternate Power Rating (Watts)
Fridge/Refrigerator 120 55
Microwave 1000 1000
Mixie 700 200
Roti Maker 30 300
我想将此数据存储到我在 CoreData 中的实体中。我刚刚创建了一个名为 EY_Appliance
的实体,其属性为 applianceId
、applianceName
、watts
和 amountPerWatts
.
最佳答案
你必须这样写代码。这将为您的实体插入具有以下属性的新对象。您可以根据需要对其进行编辑。
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]);
}
关于ios - 如何在 IOS 中将表值存储到核心数据实体中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35456721/