如何更改特定索引处键的值?

时间:2011-09-23 17:08:13

标签: iphone ios core-data

我一直试图弄清楚如何在特定索引处更改密钥的值。例如,如果我有以下数据,

Index |  slideNumber  |  Title
--------------------------------------------
1     |      5        |  test 1
2     |      2        |  test 2
3     |      5        |  test 3
4     |      7        |  test 4
5     |      9        |  test 5

如果我想在索引3处更改slideNumber的值,我该怎么做?提前谢谢!

2 个答案:

答案 0 :(得分:0)

编辑(感谢@morningstar的建议):

这可能是一种更快捷的方法:

- (void)newSlideNumber:(NSInteger)slideNumber 
              forIndex:(NSInteger)index {
    NSError *error = NULL;
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:[NSEntityDescription entityForName:@"Slides" 
                                   inManagedObjectContext:self.managedObjectContext]];

    [request setPredicate:[NSPredicate predicateWithFormat:@"Index == %d", index]];
    NSArray *results = [self.managedObjectContext executeFetchRequest:request 
                                                                error:&error];

    [request release];
    if ([results count]) {
        [(NSManagedObject *)[results objectAtIndex:0] setValue:[NSNumber numberWithInteger:slideNumber] 
                                                        forKey:@"slideNumber"];
    } else if (error) {
        NSLog(@"%@", [error description]);
    } else {
        NSLog(@"predicateWithFormat (Index == %d) did not match any objects!", index);
    }
}

原稿

你是用CoreData做的吗?如果是这样,这里有一个指南(未经测试,可能不会像复制/粘贴时那样完全正常工作。仅用作示例):

- (void)newSlideNumber:(NSInteger)slideNumber forIndex:(NSInteger)index withTitle:(NSString *)title {
    NSError *error = NULL;
    // Delete the objects first, then recreate them:
    [self deleteObjectsForEntityNamewithPred:@"MyEntityName" withPredicate:[NSPredicate predicateWithFormat:@"Index == %d", index]]; // This method is the one defined below

    // Get the managed object from your context:
    NSManagedObject *managedObjectStore = [NSEntityDescription insertNewObjectForEntityForName:@"MyEntityName" inManagedObjectContext:self.managedObjectContext];

    // Set the new values:
    [managedObjectStore setValue:[NSNumber numberWithInteger:index] forKey:@"Index"];
    [managedObjectStore setValue:[NSNumber numberWithInteger:slideNumber] forKey:@"slideNumber"];
    [managedObjectStore setValue:title forKey:@"Title"];

    // Save the context
    [self.managedObjectContext save:&error];
    if (error) {
        NSLog(@"%@", [error description]);
    }
}


- (void)deleteObjectsForEntityNamewithPred:(NSString *)entityName withPredicate:(NSPredicate *)pred {
    NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:self.managedObjectContext];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entity];
    [request setPredicate:pred];
    NSError *error = NULL;
    NSArray *results = [self.managedObjectContext executeFetchRequest:request error:&error];
    [request release];
    if (error) {
            NSLog(@"%@", [error description]);
    }
    for (NSManagedObject *managedObject in results) {
        [self.managedObjectContext deleteObject:managedObject];
    }
}

一些参考文档:

NSPersistentStoreCoordinator Class Reference

NSManagedObjectContext Class Reference

NSManagedObjectModel Class Reference


答案 1 :(得分:0)

假设您的核心数据实体有三个属性:index,slideNumber,title

您可以使用以下代码检索正确的实体(如果您考虑使用我推荐的数据库,则检查该表中的行的实例):

NSManagedObjectContext *moc = ...;
NSInteger myIndex = 3;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:moc]];
[request setPredicate:[NSPredicate predicateWithFormat:@"index == %i", myIndex]];

NSError *error = nil;
NSArray *results = [moc executeFetchRequest:request error:&error];
if (error && !results) {
  NSLog(@"Error %@\n%@", [error localizedDescription], [error userInfo]);
}
NSManagedObject *myEntity = [results lastObject];

现在我们有了实体,编辑一个值就是一个简单的KVC呼叫:

[myEntity setValue:[NSNumber numberWithInteger:5 forKey:@"slideNumber"];

从那里,您可能希望保存更改:

NSManagedObjectContext *moc = ...;
NSError *error = nil;
if (![moc save:&error]) {
  NSLog(@"Error %@\n%@", [error localizedDescription], [error userInfo]);
}

最好将Core Data视为对象图,将 NOT 视为数据库中的表。数据库只是一种持久化数据的方法。

我还建议您阅读KVC(键值编码)。如果你想通过继承你的NSManagedObjects,你可以避免使用KVC,但这是不必要的。