17978

Why can I not get a properly formatted string from NSArray element?

Question:

I'm trying to retrieve an attribute from my core data model and put the acquired string into the title of my navigation bar. I must be doing something wrong, as I cannot set my retrieved string as the title, yet I can set the title if I manually enter the string.

Here is how I am retrieving from Core Data:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Area" inManagedObjectContext:context]; [fetchRequest setEntity:entity]; //set predicate fetchRequest.predicate = [NSPredicate predicateWithFormat:@"identifier == %@", self.area_id]; NSArray *area = [context executeFetchRequest:fetchRequest error:&error]; NSString *area_name = [area valueForKey:@"name"]; NSString *test = @"test"; NSLog(@"%@",test); NSLog(@"%@",area_name);

When I set the title using self.tabBarController.title = area_name; I get "NSArrayI stringByTrimmingCharactersInSet:]: unrecognized selector sent to instance" and it sets just fine when I set it to the test variable.

I also noticed that the NSLog for the variable test is "test" whereas the NSLog output for area_name is: ( "Single Area" )

Is this because of how I am retrieving my area_name from the array? Should I be using a dictionary instead? Forgive me for my ignorance, I'm fairly new to obj-c.

Thanks.

Answer1:

Here is mistake

NSString *area_name = [area valueForKey:@"name"];

area object is type of NSArray which contains Area objects.<br /> So you need to get element from that array, and then read name.

Area *firstArea = [area firstObject]; NSString *area_name = firstArea.name; NSLog(@"%@",area_name);

Answer2:

Change the predicate to this:

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"identifier = %@", self.area_id];

I suppose than self.area_id is init correctly.

Recommend

  • Do I need a second NSFetchedResultsController
  • Accessing data in retrieved Managed Object with Core Data
  • Ordering core data objects inside a table view section
  • Issue loading subentities to mangedobject in coredata
  • How to know the last cell of the UITableView which is populating from a coredata?
  • Swift core data does not store data permanently
  • Implementing a SearchBar inside my TableView and using NSFetchedResultsController and the appropriat
  • CoreData errors for simple adding to database
  • Accessing data attribute of NSManagedObject gives me memory issues
  • How to fetch a range of items from the database
  • How to get the sm_owner User object using StackMob as backend
  • IOS Coredata check if attribute is exist or not
  • How to NSKeyedUnarchive subclasses of NSManagedObject to import into Core Data?
  • NSSortDiscriptor sort positive values
  • Using NSPredicate how to filter value from core data
  • NSManagedObjectContext executeFetchRequest returns erratic objects, causes EXC_BAD_ACCESS, SIGABRT,
  • Select columns in pandas dataframe by value in rows
  • What is the relationship between the post object, in the RestKit postObject method, and the RKMappin
  • Using Shiny actionButton() function in onRender() function of htmlWidgets
  • Cannot launch ANY process in Java (ProcessBuilder process immediately returns with exit code 128)
  • 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSMa
  • Complex Silverlight TreeView, is nested hierarchy possible?
  • Symfony2 plaintext users don't work
  • JPA CDI Injecting DAO into an Entity
  • Neo4j Cypher query performance optimization
  • How do you remove the JComboBox 'click and see dropdown' functionality?
  • Symfony2 - Doctrine - no changeset in post update
  • Cannot get text from text area
  • Doctrine/Symfony entity generator and generating entity from one table
  • Validate child input components on submit with Vee-Validate and vue js 2
  • Why cepheus don't send int without quotes to orion?
  • GAE: Way to get reference to an HttpSession from its ID?
  • AndEngine Applying Transparancy to AndEngine View
  • Force show.bind execution
  • Projection media query: browser support and workarounds?