ios - 如何在 IOS 中将表值存储到核心数据实体中

标签 ios objective-c core-data entity row

我有这样的数据:

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 的实体,其属性为 applianceIdapplianceNamewattsamountPerWatts.

最佳答案

你必须这样写代码。这将为您的实体插入具有以下属性的新对象。您可以根据需要对其进行编辑。

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/

相关文章:

iOS:数据在 Core Data 中丢失

ios - 如何在 Swift 3 中为 UIView 提供底部阴影?

ios - tableView reloadData 和 deselectRowAtIndexPath

ios - 在 swift 中为所有出现的字符串着色

iphone - UILabel 拒绝更新

objective-c - 扩展核心数据生成的 NSManagedObject 类

android - 假的 iOS UI 工具

ios - 以编程方式禁用单击 UIButton 时的突出显示

ios - 如何在现有 View Controller 之上部分加载新的 View Controller ?

ios - TableView View Controller 显示下一个 View