6
[newVehicle setValue: _txtFieldVehicleNumber.text forKey:@"number"];

[newVehicle setValue: lblFuelType.text forKey:@"fueltype"];

[newVehicle setValue: lblFuelUnit.text forKey:@"fuelunit"];

[newVehicle setValue: lblDistanceUnit.text forKey:@"distanceunit"];

我想更新名为“Vehicle”的核心数据实体,对于该实体,我有几个属性,当我从实体中选择特定属性时,我想更新其中一些属性,但不是全部。所以我该怎么做 ??

4

2 回答 2

17

您可以执行以下操作(近似代码。实现错误处理和检查语法)。

NSFetchRequest *fetchRequest=[NSFetchRequest fetchRequestWithEntityName:@"Vehicle"];
NSPredicate *predicate=[NSPredicate predicateWithFormat:@"vehicle_id==%@",vehicle_id]; // If required to fetch specific vehicle
fetchRequest.predicate=predicate;
Vehicle *newVehicle=[[self.managedObjectContext executeFetchRequest:fetchRequest error:nil] lastObject];

[newVehicle setValue: _txtFieldVehicleNumber.text forKey:@"number"];

[newVehicle setValue: lblFuelType.text forKey:@"fueltype"];

[newVehicle setValue: lblFuelUnit.text forKey:@"fuelunit"];

[newVehicle setValue: lblDistanceUnit.text forKey:@"distanceunit"];

[self.managedObjectContext save:nil]
于 2013-09-27T14:52:32.613 回答
3

从核心数据中获取实体:

VVdAppDelegate *delegate = (VVdAppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *managedObjectContext = [delegate managedObjectContext];
NSEntityDescription *entityDescriptionDelegate = [NSEntityDescription entityForName:@"entityName" inManagedObjectContext:managedObjectContext];
NSFetchRequest *requestData = [[NSFetchRequest alloc] init];
[requestData setEntity:entityDescriptionDelegate];
NSError *error;

如果有多个 NSManagedObject

NSArray *objectsArray = [managedObjectContext executeFetchRequest:requestData error:&error];

for (NSManagedObject *object in objectsArray) {

        // Here update values of every object

}

// And save values to core data
[managedObjectContext save:&error];
于 2013-09-27T14:52:40.067 回答