1

現在、NSManagedObjectContext内のすべてのオブジェクトをクリアする次のコードがあります。

- (void)clearObjectList:(NSString *)identifier
{
    // TODO: Delete any entries with the identifier at the start of the object's name
    NSLog(@"Clearing the URL list...");

    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    NSFetchRequest * fetch = [[NSFetchRequest alloc] init];
    [fetch setEntity:[NSEntityDescription entityForName:@"URL" inManagedObjectContext:context]];
    NSArray * result = [context executeFetchRequest:fetch error:nil];
    for (id basket in result)
    {
        // Code here to check if we should delete this object
        [context deleteObject:basket];
    }

    NSError *error = nil;
    if (![context 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();
    }
}

私が持っているURLのデータモデルは次のとおりです。

dateAccessed: Date
        name: String
         url: String

nameオブジェクトキーにアクセスして、削除する必要があるかどうかを判断したいと思います。これにアクセスするにはどうすればよいですか?

4

2 に答える 2

2

これを試して:

for(URL *basket in result)
{
    if([basket.name isEqualToString:identifier])
        [context deleteObject:basket];
}
于 2012-05-31T20:08:37.883 に答える
1

結果をループすると、返されるのはsだけであることがわかりますNSManagedObject。または、NSManagedObjectサブクラスを作成した場合は、URLオブジェクトのみが返されます。したがって、 forループのidいずれかNSManagedObject *またはで置き換えることができます。URL *

私がお勧めするサブクラスを作成した場合はname、ドット表記でアクセスできますbasket.name。そうでない場合は、を呼び出すことでアクセスできます[basket valueForKey:@"name"]

于 2012-05-31T20:06:49.023 に答える