0

Given an NSArrayController "objController" which is bound to the moc in IB, shouldn't the following work? And given that it doesn't seem to work, how do I go about retrieving an entity's name from my NSArrayController?

for (NSManagedObject *thisObj in [objController arrangedObjects]) 
{
    NSEntityDescription *description = [thisObj entity];
    NSString *entityName = [description name];
    // do something with entityName...
    NSString *entityAttributeValue = [thisObj valueForKey:@"attributeKey"];
    // do something with entityAttributeValue...
}

The "objController" is IBOutlet-ed and set to "Entity Name" Mode, with the entity name set to an entity defined in the model. This entity does have child entities (and thus the reason I would like to access its description name, since the NSArrayController could store many different child entity types), but the presence of child entities doesn't seem to make a difference anyway.

... in the debugger, it looks like "description" is a valid NSEntityDescription object, but "entityName" gets set to a _PFEncodedString object, with no content. However, "entityAttributeValue" is just fine, populated with the correct value stored in Core Data.

One way around this, I suppose, would be to custom-class all my CD entities, then use -isKindOfClass to get the information I need.

Any ideas?

Eimantas
  • 48,927
  • 17
  • 132
  • 168
Sam Hatchett
  • 513
  • 5
  • 11
  • `_PFEncodedString` is an (internal) subclass of `NSString`. What do you mean by "entityName has no content"? – Martin R Sep 04 '12 at 18:32
  • I realize that `_PFEncodedString` is internal - what I mean to say is that the debugger does not show any interpreted string at that location as I have seen in some other projects. It's just a bit mysterious that a documented method would return a `_PFEncodedString`... – Sam Hatchett Sep 04 '12 at 18:53
  • No, that is quite normal. For example `NSString *s = @"123";` results in a `__NSCFConstantString`. - But "po entityName" in the debugger should show the string! – Martin R Sep 04 '12 at 18:57
  • of course you are right. Ok, this head-scratcher became a bit of a face-palm. The debugger variable-view was being deceptive in not showing me an interpreted string for that object, but I was being foolish in trying to call `-UTF8String` on the entity name and pass that directly into a char* c function. The answer was to alloc-init a new string with the contents of `[[managedObject entity] name]`. thanks, Martin, for your help. – Sam Hatchett Sep 04 '12 at 19:26
  • You do not have to create a new string. `[description name]` returns a n instance of a `NSString` subclass and can be used just as any other string. – Martin R Sep 04 '12 at 19:35
  • I just tested it: `const char *name = [[[object entity] name] UTF8String];` just works. – Martin R Sep 04 '12 at 19:38
  • ... and of course you are right again. there must have been something else going on in my code that i didn't realize i fixed -- i was definitely getting bad_access errors when my (third party) C library tried to use the passed-in utf8 string. it's a mystery. thanks again. – Sam Hatchett Sep 04 '12 at 20:59

1 Answers1

1

_PFEncodedString is a (private) concrete subclass of NSString and can therefore be used as any other NSString.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Thanks - I would just add for anyone scouring the google that Xcode's variables view does not always correctly interpret the contents of some objects, so be advised to use the interactive debugger console before posting silly questions on SO ;) – Sam Hatchett Sep 05 '12 at 17:50