使用核心数据更新属性

时间:2013-09-05 18:11:01

标签: iphone ios core-data nsfetchrequest

我有一个具有3个属性的实体:firstname,lastname和score。我可以通过名字和姓氏搜索一个人的分数并将其显示在UILabel上(到目前为止一直很好)我的问题是我不知道如何用新值更新分数属性。在我用来执行搜索并显示旧值的代码下面。

NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"Scorecard" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:entitydesc];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstname like %@ and lastname like %@", self.firstnameTextField.text, self.lastnameTextField.text];
[request setPredicate:predicate];

NSError *error;
NSArray *integer = [context executeFetchRequest:request error:&error];

if(integer.count <= 0){
    self.displayLabel.text = @"No records found";

}

else {

    NSNumber *score;

    for (NSManagedObject *object in integer) {


       score = [object valueForKey:@"score"];

        int x = [score intValue];

       // New Value is going to be the old value + 2
        int y = x + 2;

      //displays old value found
        [self.displayLabel setText:[NSString stringWithFormat:@"old value %d",x]];

    }

}

更新#1

我能够更新属性并保存它但是我必须将新的Integer值转换为字符串然后将其转换回整数。我不喜欢额外的一步。如果有人有其他建议,请告诉我并感谢您的帮助。在更新的代码下面。

 NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"Scorecard" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:entitydesc];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstname like %@ and lastname like %@", self.firstnameTextField.text, self.lastnameTextField.text];
[request setPredicate:predicate];

NSError *error;
NSArray *integer = [context executeFetchRequest:request error:&error];

if(integer.count <= 0){
    self.displayLabel.text = @"No records found";

}

else {

    NSNumber *score;

    for (NSManagedObject *object in integer) {

        score = [object valueForKey:@"score"];

        int x = [score intValue];
        int y = x + 2;
        NSString* z = [NSString stringWithFormat:@"%d",y];

        [object setValue:[NSNumber numberWithInteger:[z integerValue]] forKey:@"score"];
        [self.displayLabel setText:[NSString stringWithFormat:@"Value updated to %d",y]];
        [context save:&error];

    }

}

更新2

感谢Kevin的回答,我能够解决我的问题。我现在能够用我的核心数据上的属性替换另一个整数值的整数值。在我的代码下面终于工作了。

NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"Scorecard" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:entitydesc];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstname like %@ and lastname like %@", self.firstnameTextField.text, self.lastnameTextField.text];
[request setPredicate:predicate];

NSError *error;
NSArray *integer = [context executeFetchRequest:request error:&error];

if(integer.count <= 0){
self.displayLabel.text = @"No records found";

}

else {

NSNumber *score;

for (NSManagedObject *object in integer) {

    score = [object valueForKey:@"score"];

    int x = [score intValue];
    int y = x + 2;

    [object setValue:[NSNumber numberWithInt:y] forKey:@"score"];
    [self.displayLabel setText:[NSString stringWithFormat:@"Value updated to %d",y]];
    [context save:&error];

}

}

2 个答案:

答案 0 :(得分:1)

获取之前的对象,然后设置它的得分属性

[object setValue:[NSNumber numberWithInt:y] forKey:@"score"]; 

答案 1 :(得分:0)

我的第一个建议是使用NSManagedObject的子类而不是使用原语。您可以使用核心数据检查器并更改实体类来完成此操作。

现在,要对分数进行更改,您需要找到需要更改的对象,并分配值。分配值后,您需要保存对象所属的NSManagedObject。上下文成功保存后,您的更改将持续存在。