私はcoreDataを初めて使用し、「Test」というエンティティにdisplayOrderというfloat型属性を追加したサンプルアプリケーションに取り組んでいます。各テスト オブジェクトを追加するたびに、この表示順序を増やしたいと考えています。これが起こるために、私は以下のコードを書きましたが、うまくいきません。どこが間違っているのか知りたいです
- (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になります。
よろしくランジット