1

我有一个具有 3 个属性的实体:名字、姓氏和分数。我可以通过名字和姓氏搜索一个人的分数并将其显示在 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

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

 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

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

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];

}

}
4

2 回答 2

1

Get an object like you did before then set's it's score property

[object setValue:[NSNumber numberWithInt:y] forKey:@"score"]; 
于 2013-09-05T18:49:36.723 回答
0

我的第一个建议是使用 NSManagedObject 的子类而不是使用原语。您可以通过使用核心数据检查器并更改您的实体类来做到这一点。

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

于 2013-09-05T19:13:10.003 回答