1

I am fetching data from core data and trying to print the name of the object at valueForKey. This has never caused me any trouble however when getting a direct object but the object I am getting is from an NSSet relationship.

When I print out the value I want, it displays in this format.

{(
     Bob Marley
)}
{(
    Jack Daniels
)}
etc...

I have used NSString stringWithFormat, I have tried componentsSeperatedByString, however neither of these work. If another question has been asked, I cannot find it on here. The issue is identical in a UILabel as well. Makes no difference where I print it.

Hopefully it's a simple issue.

My code for getting the value is.

//Company * company
//Employee is related in a many-many relationship
NSString * name = (NSString *)[self.company.contractor valueForKey:@"contractorName"];
        NSLog(@"Name: %@", name);

Thanks.

9
  • you should use managed object to retrieve the data from core data that will help Commented Nov 12, 2014 at 13:23
  • I am using a fetchedResults controller to get the results, how would using managedObject make a difference? I have never had to use it as fetchedResults get's me what I have needed in the past. Commented Nov 12, 2014 at 13:25
  • you are getting array inside array{( Bob Marley )} where {(Arry)} Commented Nov 12, 2014 at 13:28
  • Why are you using valueForKey? Commented Nov 12, 2014 at 13:35
  • To get a specific name of an attribute in the Entity Contractor. Is there a better solution? Commented Nov 12, 2014 at 13:36

1 Answer 1

2

self.company.contractor is NSArray.

So you need to do

NSString * name = (NSString *)[[[self.company.contractor allObjects] firstObject] valueForKey:@"contractorName"];  

Also refer to my previous answer

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, that is not correct. self.company.contractor is an NSSet not NSArray. So firstObject does not work. This is the message when trying your solution: "No visible @interface for 'NSSet' declares the selector 'firstObject'"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.