【发布时间】:2011-09-30 19:19:54
【问题描述】:
由于某种原因,所有 NSString 类型的属性都在我的 Article 对象中作为 NSArrays 返回。这是我检索它们的函数:
- (NSArray *)getSavedArticles
{
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
NSError *error = nil;
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Article" inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
NSSortDescriptor *dateSort = [NSSortDescriptor sortDescriptorWithKey:@"last_opened" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:dateSort]];
NSArray *fetchedObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
return fetchedObjects;
}
我没有为这些创建 NSManagedObjectModel,而是使用 KVO 访问它们。
//self.data has the array returned from getSavedArticles
NSManagedObject *object = [self.data objectAtIndex:indexPath.row];
NSString *path = [object valueForKey:@"path"];
NSString* id = [object valueForKey:@"id"];
当我在变量窗格中查看此内容时,在path 和id 我看到(__NSArrayI *) ... Variable is not a CFString. 打印其中任何一个的描述也会打印出打印数组时使用的括号。
我只是想知道这是否可能是由于排序描述符?我已经仔细检查了进入这些对象的数据是否输入正确,并且我用来备份所有内容的 SQLite 数据库正在显示字符串。
我已尝试在模拟器中重新安装该应用程序并重置它。我仍然得到一个 NSArray 而不是 NSString。
我真的不知道这里发生了什么。任何帮助将不胜感激。
编辑:我刚刚发现了一些有趣的东西。我再次检查文章是否已保存,这次我没有得到文章路径的 NSArray:
- (NSString *)hasArticleSaved:(NSString *)id
{
NSString *path = nil;
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"%K == %@", @"id", id]];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Article" inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
NSError *error = nil;
NSArray *fetchedObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects.count > 0) {
NSManagedObject *savedArticle = [fetchedObjects objectAtIndex:0];
path = [savedArticle valueForKey:@"path"];
}
return path;
}
现在我真的很困惑。有接盘侠吗?
【问题讨论】: