我是 coreData 的新手,我正在开发一个示例应用程序,在该示例应用程序中,我向一个名为“Test”的实体添加了一个名为 displayOrder 的浮点类型属性。我想在添加每个测试对象时增加这个显示顺序。为此,我编写了以下代码,但它不起作用,我想知道我哪里出错了
- (void)insertNewObject:(NSString *)fileName
{
Test *testName = [NSEntityDescription insertNewObjectForEntityForName:@"Test" inManagedObjectContext:self.managedObjectContext];
[testName setName:fileName];
NSManagedObject *lastObject = [self.controller.fetchedObjects lastObject];
float lastObjectDisplayOrder = [[lastObject valueForKey:@"displayOrder"] floatValue];
NSLog(@"%f",lastObjectDisplayOrder);
[testName setValue:[NSNumber numberWithDouble:lastObjectDisplayOrder + 1.0] forKey:@"displayOrder"];
// Save the context.
NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
实际上,问题是它第一次递增,即从 0 到 1,但在此之后我总是得到 1。
问候兰吉特