Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 从NSTreeController中删除具有特定标题的对象_Objective C_Cocoa_Core Data - Fatal编程技术网

Objective c 从NSTreeController中删除具有特定标题的对象

Objective c 从NSTreeController中删除具有特定标题的对象,objective-c,cocoa,core-data,Objective C,Cocoa,Core Data,我想知道如何根据CoreData“name”属性的标题删除对象。 要添加对象,我使用以下代码: NSManagedObjectContext *moc = [self managedObjectContext]; JGManagedObject *theParent = [NSEntityDescription insertNewObjectForEntityForName:@"projects" inManagedO

我想知道如何根据CoreData“name”属性的标题删除对象。 要添加对象,我使用以下代码:

NSManagedObjectContext *moc = [self managedObjectContext];
JGManagedObject *theParent = 
    [NSEntityDescription insertNewObjectForEntityForName:@"projects"
                                  inManagedObjectContext:moc];
[theParent setValue:nil forKey:@"parent"];
// This is where you add the title from the string array
[theParent setValue:@"myTitle" forKey:@"name"]; 
[theParent setValue:[NSNumber numberWithInt:0] forKey:@"position"];

但我似乎找不到一个等价的函数来移除一个对象

我不知道您是否查阅了“核心数据编程指南”中的对象部分

编辑

我修改了它,将其从名称数组中删除。再一次不到5分钟的工作时间

2009年10月10日编辑-添加Joshua尝试过的内容

注释

我突出显示了两行

您已经将我的示例粘贴为for循环,而不是函数调用。这只是一次取下一个字符串并将其传递到方法中。在我的示例中,我传入了一个要匹配的字符串数组

这就是你的问题所在。如果您费心阅读谓词编程指南(位于顶部的谓词基础部分),它会说它希望它所使用的类应该是符合KVC的。这就是为什么会出现有关KVC合规性的错误。您正在尝试在中搜索标题。。。但标题不是您模型的属性

我想你可能会对谓词的作用感到困惑。看看我写的示例代码

首先,我创建一个Fetch请求,它将从“Projects”实体中选择对象

其次,我创建了一个谓词,表示对于fetch请求返回的每个对象,获取'name'属性的值,并将其与'namesArray'中对象的值进行比较

第三,我正在创建一个排序描述符,它将根据“name”属性按升序对结果进行排序


然后,一旦我设置了这个获取请求,我就在moc上运行它,它返回一个符合这些条件的对象数组。

谢谢,这正是我想要的。我正在尝试删除多个对象,所以就像你说的那样,这会有点困难。我从哪里开始呢?啊,好的,这很有帮助。这是我正在使用的代码。它循环遍历一个字符串数组,并希望删除标题与数组中任何字符串匹配的对象。但它似乎抛出了大量错误。它表示该类不符合KVC。但是我不确定它在说什么。@Joshua:这些屏幕抓取将在一周内消失,这对其他有同样问题的人来说没有任何帮助。首先,您已经将我的方法定义复制并粘贴为for循环。其次,这不是一个错误负载,这是单个错误的调用堆栈,它向您显示了导致问题的方法调用。第三;您正在尝试基于属性标题创建谓词,该属性标题在您的模型中不存在。确定。我希望谓词基于数组中的字符串而不是属性;它需要一个名为title的属性,在您的模型中找不到该属性。
- (void)removeObjectsWithNames:(NSArray *)nameArray {
    // Get the moc and prepare a fetch request for the required entity
    NSManagedObjectContext *moc = [self managedObjectContext];
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:moc];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDescription];

    // Create a predicate for an array of names.
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name IN %@", nameArray];
    [request setPredicate:predicate];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];

    // Execute the fetch request put the results into array
    NSError *error = nil;
    NSArray *resultArray = [moc executeFetchRequest:request error:&error];
    if (resultArray == nil)
    {
        // Diagnostic error handling
        NSAlert *anAlert = [NSAlert alertWithError:error];
        [anAlert runModal];
    }

    // Enumerate through the array deleting each object.
    // WARNING, this will delete everything in the array, so you may want to put more checks in before doing this.
    For (JGManagedObject *objectToDelete in resultArray ) {
        // Delete the object.
        [moc deleteObject:objectToDelete];
    }
}
for(NSString *title in oldTasks) { // 1
    // Get the moc and prepare a fetch request for the required entity
    NSManagedObjectContext *moc = [self managedObjectContext];
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"projects" inManagedObjectContext:moc];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDescription];

    // Create a predicate for an array of names.
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title IN %d", oldTasks]; // 2
    [request setPredicate:predicate];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];

    // Execute the fetch request put the results into array
    NSError *error = nil;
    NSArray *resultArray = [moc executeFetchRequest:request error:&error];
    if (resultArray == nil)
    {
        // Diagnostic error handling
        NSAlert *anAlert = [NSAlert alertWithError:error];
        [anAlert runModal];
    }

    JGManagedObject *objectToDelete = [resultArray objectAtIndex:0];
    // Delete the object.
    [moc deleteObject:objectToDelete];
}