【问题标题】:Saved string in CoreData returns <null> and the string. Why?CoreData 中保存的字符串返回 <null> 和字符串。为什么?
【发布时间】:2012-06-20 15:40:47
【问题描述】:

您好,我将来自数据库的值保存在 MultableArray 中,然后保存在 CoreData 中:

 NSMultableArray *mutary = [[NSMultableArray alloc] init];
      NSManagedObjectContext *context = [app managedObjectContext];
    for(int n=0; n<[AttributeArray count]; n++)
    {

    [mutary addObject:[[AttributeArray objectAtIndex:n] objectForKey:@"AttributName"]];
    NSLog(@"%@", mutary);
}
attributeString = [mutary componentsJoinedByString:@","];

 raume = [NSEntityDescription insertNewObjectForEntityForName:@"Raum" inManagedObjectContext:context];

raume.raumattribut = attributeString;

 if (![context save:&error]) {
 NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
 abort();

}

MultableArray 的 NSLog 输出为:

    2012-06-20 17:21:00.047 book-app[31984:15803] (
    A7OVERHEAD,
    Beamer
)

到目前为止,它的工作正常。数据库中的两个预期值现在位于数组中。 现在我从 CoreData 获取这些属性:

    NSManagedObjectContext *context = [app managedObjectContext];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Raum" inManagedObjectContext:context];
    [request setEntity:entity];
    NSError *error = nil;
    NSArray *events = [context executeFetchRequest:request error:&error];

for (Raum *att in events)
    {
     stringAttribute = [[events valueForKey:@"raumattribut"] componentsJoinedByString:@","];
     NSLog(@"ATTRIBUTE: %@", stringAttribute);
    }
}

到目前为止一切顺利。但如果我现在查看我的 NSLog 输出:

2012-06-20 17:21:00.055 book-app[31984:15803] ATTRIBUTE: <null>,A7OVERHEAD,Beamer

CoreData 正在返回,然后是两个值。这是哪里来的?

有人可以帮忙吗?

提前致谢

【问题讨论】:

    标签: iphone objective-c ios core-data


    【解决方案1】:

    编辑:经过一些调查和澄清(见 cmets),这是我的答案: 在 NSArray 上调用 valueForKey 会返回一个数组,其中每个元素都是通过在原始数组的每个成员上调用 valueForKey 来创建的。这意味着您的输出表明您的 fetch 请求返回了 3 个对象,而第一个对象没有设置它的 raumattribut 属性。

    以前的答案: 您在 NSArray (查询结果)的实例上调用 valueForKey 。也许您需要先 get objectAtIndex:0 使用您正在迭代值的变量? valueForKey 可能返回 nil。

    stringAttribute = [[att valueForKey:@"raumattribut"] componentsJoinedByString:@","];
    

    或类似的。

    编辑: 一开始我错过了第二部分的for循环,我编辑了上面的代码示例以使用attr,它

    【讨论】:

    • 如果我这样做,它只返回(null)。还有其他解决方案吗?
    • 我已经编辑了我的答案。虽然我不确定 raumattribut 属性是什么类型,以及 componentsJoinedByString 在那里是否合适。
    • 这不起作用。我得到错误:-[NSCFString componentsJoinedByString] 无法识别的选择器..“raumattribute”是一个 NSString,为什么你调用 [att valueForKey:@"raumattribut"] 而不是 [events valueForKey:@"raumattribut"]
    • events 是 NSManagedObjects 的 NSArray,因此应该将 nil 返回给发送给它的任何 valueForKey。我怀疑 raumattribute 不是一个数组,并且 componentsJoinedByString 不起作用。尝试在循环中记录 NSLog(@"%@", att) 。这应该会在 NSManagedObject 的数据中显示所有值及其键。
    • 好的,但是我已经完成了这个 stringAttribute = [[events valueForKey:@"raumattribut"] componentsJoinedByString:@","];我的 NSLog 的输出是 ATTRIBUTE: ,A7OVERHEAD,Beamer。所以它不为空,只有返回的第一个值是 .
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 2012-10-09
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 2011-11-04
    相关资源
    最近更新 更多