Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6163083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:45:54+00:00 2026-05-23T21:45:54+00:00

I’m able to get things working fine with Core Data and to achieve my

  • 0

I’m able to get things working fine with Core Data and to achieve my desired results, but I always feel it very awkward when walking to-many relationships because NSSet is, for my typical purposes, fairly useless.

An example is if I have obtained a NSManagedObject of Entity “Zoo” with attribute “nameOfZoo” and to-many relationship “animalCages”, the to-many relationship pointing to Entity “AnimalCage” which has attribute “nameOfSpecies” and to-many relationship pointing to Entity “IndividualAnimal”

Zoo [nameOfZoo] ->> AnimalCage [nameOfSpecies] ->> Animals

So, getting the top level Zoo object, that’s simple. But then I want to get the data for nameOfSpecies “Canus Lupus”. The code I want to write is this:


// Normal NSEntityRequest or whichever it is, I have no gripe with this
NSManagedObject *zoo = ..the request to get the one Zoo..;

// I want to get the object where the key "nameOfSpecies" is set to "CanusLupus" NSManagedObject *wolf = [[zoo animalCages] object:@"Canus Lupus" forKey:@"nameOfSpecies"];

Obviously, I can't obtain wolf in this manner. Instead, I have to write like 10 lines of code (feels like 100 lines of code) to first obtain the set, then set up a search predicate request, and declare an error variable, execute the request, then get an array of the results, then get the first element of that array.. and if I want to walk further down the tree, to find the animal named "Wolfy" for instance, then I have to do it all over again.

Am I doing things correctly or am I foolishly an easier way? I guess I can put a category on NSSet, maybe I will, but I feel like there should be a built in better way. If not, why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:45:55+00:00Added an answer on May 23, 2026 at 9:45 pm

    If you have a data model like this:

    Zoo{
      name:string
      animalCages<-->>AnimalCage.zoo
    }
    
    AnimalCage{
      nameOfSpecies:string
      zoo<<-->Zoo.animalCages
      animals<-->>Animal.animalCage
    }
    
    Animal{
      name:string
      animalCage<<-->AnimalCage.animals
    }
    

    The to find a specific AnimalCage by name of species for a given Zoo object:

      NSString *soughtSpecies=@"Canis Lupis"; // normally this variable would be passed in to the method
      NSPredicate *p=[NSPredicate predicateWithFormat:@"nameOfSpecies==%@", soughtSpecies];
      NSSet *wolves=[[zoo animalCages] filteredSetUsingPredicate:p];
    

    Or you can use objectPassingTest: if you like blocks.

    If you use custom NSManagedObject subclasses, then you get custom accessor methods and can use self.dot notation so the above would be:

      Zoo *zoo= // fetch the appropriate zoo object
      NSString *soughtSpecies=@"Canis Lupis";
      NSPredicate *p=[NSPredicate predicateWithFormat:@"nameOfSpecies==%@", soughtSpecies];
      NSSet *wolves=[zoo.animalCages filteredSetUsingPredicate:p];
    

    If you know before hand that you are going to have to find cages a lot, you could wrap the above up in a method on your Zoo class e.g.

    @implementation Zoo
    //... other stuff
    -(AnimalCage *) cageForSpecieNamed:(NSString *) specieName{
      NSPredicate *p=[NSPredicate predicateWithFormat:@"nameOfSpecies==%@", specieName];
      NSSet *wolves=[self.animalCages filteredSetUsingPredicate:p];
      return [wolves anyObject]; // assuming one cage per species
    }
    

    Objective-c is intentionally verbose because it was supposed to be “self documenting” and the editor written for the language at the beginning had autocomplete. So, if your used to a more expressive language it might seem you are doing a lot of work but logically you aren’t.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.