我正在使用 foliowin 函数来获取应用程序中的所有记录
-(NSArray*)recordsInTable:(NSString*)tableName andManageObjectContext:(NSManagedObjectContext *)manageObjContext
{
NSError *error=nil;
// **** log objects currently in database ****
// create fetch object, this object fetch's the objects out of the database
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName: tableName inManagedObjectContext:manageObjContext];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [manageObjContext executeFetchRequest:fetchRequest error:&error];
return fetchedObjects;
}
并使用以下代码获取记录
NSArray *records=[dbAccessObj recordsInTable:@"Details" andManageObjectContext:managedObjectContext];
NSLog(@"records %@",records);
我收到这样的日志
**records (
"<Details: 0x76374b0> (entity: Details; id: 0x7636730 <x-coredata://CDEF0797-F141-47A7-902D-9689DCD90B83/Details/p1> ; data: <fault>)",
"<Details: 0x7637770> (entity: Details; id: 0x7636740 <x-coredata://CDEF0797-F141-47A7-902D-9689DCD90B83/Details/p2> ; data: <fault>)",
"<Details: 0x7637870> (entity: Details; id: 0x7636750 <x-coredata://CDEF0797-F141-47A7-902D-9689DCD90B83/Details/p3> ; data: <fault>)"
)**
但我想显示包含所有详细信息的日志,如下所示。我犯了什么错误?
**{
age = 11;
fName = "firstName";
lName = "lastname";
sId = 4;
})**
最佳答案
您的代码没有错误。您在数组中看到的是“故障”。来自核心数据编程指南:
Faulting is a mechanism Core Data employs to reduce your application’s memory usage.
... A fault is a placeholder object that represents a managed object that has not yet been fully realized, or a collection object that represents a relationship.
... If at some stage a persistent property of a fault object is accessed, then Core Data automatically retrieves the data for the object and initializes the object.
executeFetchRequest:
返回错误数组(= 占位符对象)以节省内存。一旦您检索到对象的属性,该对象就会被完全初始化并且所有属性在调试器中都是可见的。
对于调试目的(仅),您可以通过以下方式强制 Core Data 完全初始化数组中的所有对象
NSArray *allAges = [records valueForKey:@"age"];
NSLog(@"records %@",records);
关于ios - 如何在 CoreData 的 NSLog 中显示详细信息?建议我一个解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12399910/