Core-Data: NSLog output Does Not Show “Fields”

一笑奈何 提交于 2019-12-05 10:49:42

To view the individual fields of the objects in the array you must reference them directly. You are using this to show the objects in the array:

NSLog (@"Element %i = %@", i, [stories objectAtIndex: i]);

NSLog generates a description of each object when you use %@, which is often convoluted or useless. If you want to see the fields inside try something like this:

NSLog (@"Element %i contains fields: %d - %s - %d",i,[[stories objectAtIndex: i] getIntegerField],[[stories objectAtIndex: i] getStringField],[[stories objectAtIndex: i] getAnotherIntegerField]);

The values displayed will be the values returned by the getter functions of each object you are referencing in stories.

The output you are seeing is from the description method of the NSManagedObject class. It is just a human readable output intended for lightweight debugging purposes. You're not supposed to actually use it to parse or store data.

You don't see any detail about the data in the description dump because the managed objects were fetched as "faults" which means they are mere ghost of objects and contain no real data. To fetch the full fledged objects immediately, you would set you fetch request to do so with:

[request setReturnsObjectsAsFaults:NO];

... then when you log the objects you will see their data and relationships.

To actually use the data in the attributes, you have to query the attributes directly using one of the value methods such as valueForKey:@"attributeName".

Edit:

After editing your question, I think I need to elaborate.

You're thinking about Core Data all wrong. Core Data is not a database. It does not have fields. The return of a fetch request is not a table.

The relational database elements in Core Data are utterly optional and hidden. Instead, Core Data is an object graph manager that maintains the integrity of relationships between objects. The data is stored in objects and when you fetch from the persistent store you get back an object, not a field, column or row.

In this case, the fetch is returning a generic NSManagedObject that is configured to represent an entity Tabrss in your entity graph. Each managed object instance represents one logical Tabrss object. To get any value from from any Tabrss object you ask the managed object for the value associated with the name of the attribute of the Tabrss entity.

Suppose your Tabrss entity has a name attribute. To get the name of every Tabrss in your fetch you would use:

int i;
for (i = 0; i < arrayItemQuantity; i++)
    NSLog (@"Element %i = %@", i, [[stories objectAtIndex: i] valueForKey:@"name"]);

Or making it more explict:

NSManagedObject *aTabrss;
for (aTabrss in stories)
    NSLog(@"aTabrss.name=%@",[aTabrss valueForKey:@"name"]);

If you make a custom NSManagedObject subclass for the Tabrss entity you can query the attribute directly using the dot notation:

TabrssSubclass *aTabrss;
for (aTabrss in stories)
    NSLog(@"ATabrss.name=%@",aTabrss.name]);

The important thing it to remember you are dealing with full fledged objects and not arrays, matrixes, tables or some other dumb data structure. You get the data from from each individual object by sending a message asking for the value of one its attributes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!