我正在Entity
使用属性EY_Appliances
,applianceId
,applianceName
,watts
调用amountPerWatts
。此外我还有像这样的数组:
Prefrence.h
#define APPLIANCENAME_ARRAY @[@"Fridge",@"Microwave",@"Mixie",@"Roti Maker",@"Coffee Machine",@"Dish Washer",@"Wet Grinder",@"Electric Stove",@"Ceiling Fan",@"TV",@"Table Fan",@"Tubelight",@"Bulb",@"AC",@"Vacuum Cleaner",@"CFL",@"LED",@"Washing Machine",@"Toaster",@"Room Heater",@"Iron",@"Motor",@"Water Heater",@"Inverter / UPS",@"Air Cooler",@"Steamer / Air Fryer",@"Hair Dryer",@"Laptop",@"PC",@"Tablet",@"Router / Modem",@"Home Theatre",@"Projector",@"PS3/PS4/XBOX"]
#define WATTS_ARRAY @[@"120",@"1000",@"700",@"30",@"167",@"810",@"180",@"150",@"75",@"120",@"75",@"40",@"60",@"1200",@"1400",@"20",@"6",@"300",@"1000",@"1600",@"1400",@"2400",@"1000",@"67",@"173",@"585",@"1026",@"15",@"150",@"4",@"4",@"17",@"240",@"10"]
DataAccessHandler.m
-(void)storeApplianceDetailsToEntity
{
NSManagedObjectContext *context = [self managedObjectContext];
for (int i=0; i<APPLIANCENAME_ARRAY.count; i++)
{
NSManagedObject *object = [NSEntityDescription insertNewObjectForEntityForName:@"EY_Appliances" inManagedObjectContext:context];
[object setValue: APPLIANCENAME_ARRAY[i] forKey:@"applianceId"];
[object setValue: APPLIANCENAME_ARRAY[i] forKey:@"applianceName"];
[object setValue: WATTS_ARRAY[i] forKey:@"watts"];
[object setValue: WATTS_ARRAY[i] forKey:@"amountPerWatts"];
}
NSError *error = nil;
if (![context save:&error])
{
NSLog(@"Saving Failed with error %@", error);
}
NSLog(@"entityValue==>%@",context);
}
-(NSArray *) fetchApplianceDetailsFromEntity:(NSString *) entityName
{
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest;
NSEntityDescription *entityDescription;
NSArray *array;
fetchRequest = [[NSFetchRequest alloc] init];
entityDescription = [NSEntityDescription entityForName:entityName inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entityDescription];
array = [managedObjectContext executeFetchRequest:fetchRequest error:nil];
NSLog(@"arr==>>%@",array);
return array;
}
ViewController.m
-(void)viewDidLoad
{
DataAccessHandler *dataAccess=[[DataAccessHandler alloc]init];
[dataAccess storeApplianceDetailsToEntity];
NSArray *applianceData = [dataAccess fetchApplianceDetailsFromEntity:@"EY_Appliances"];
//How to print the applianceName,If i write Like this applianceData.applianceName,it shows error…
}
1.如何打印applianceName?
2.如何设置applianceId primaryKey并将applianceId 1的值存储到34?
答案 0 :(得分:0)
NSDictionary *applianceData = [dataAccess fetchApplianceDetailsFromEntity:@"EY_Appliances"];
使用关键字返回值
答案 1 :(得分:0)
applianceData
是一个数组,因此您需要通过索引
您的代码刚刚使用设备名称作为ID - 如果您只需要一个标识符,则可以使用索引i
加载会将所有内容返回到数组中 - 如果您只想加载部分数据集,则需要在fetchApplianceDetailsFromEntity
中添加获取谓词
[fetch setPredicate:[NSPredicate predicateWithFormat:@"(applianceId == %@)",idString]];