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 8102961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:23:04+00:00 2026-06-05T23:23:04+00:00

In this test Core Data project, I have a one to many relationship from

  • 0

In this test Core Data project, I have a one to many relationship from “Customer” to “Products” and this relationship is named ‘products’. Customer’s attribute is ‘name’ and Product’s attribute is ‘item’. I’ve subclassed the entities and Xcode has produced the following for Customer:

@interface Customer : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSSet *products;
@end

@interface Customer (CoreDataGeneratedAccessors)

- (void)addProductsObject:(Products *)value;
- (void)removeProductsObject:(Products *)value;
 - (void)addProducts:(NSSet *)values;
- (void)removeProducts:(NSSet *)values;

@end

To add, let’s say, one customer (John Doe) and two items (Widget 1 & Widget 2), I can use the accessor method addProductsObject as follows:

...
// Add (1) customer object
Customer *custObj = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
[custObj setValue:@"John Doe" forKey:@"name"];

// Add (2) products for John Doe
for (int foo=0; foo<2; foo++) {
    self.product = [NSEntityDescription insertNewObjectForEntityForName:@"Products" inManagedObjectContext:context];
    NSString *objString = [NSString stringWithFormat:@"Widget %d", foo];
    self.product.item = objString;
    [custObj addProductsObject:self.product];
}
...

This works fine but, if possible, I’d like to make use of the addProducts accessor.

I’m assuming that the generated accessor method addProducts is there to facilitate the insertion of a ‘set’ of objects with something like:

...
NSSet *itemSet = [[NSSet alloc] initWithObjects:@"Widget 1", @"Widget 2", nil];
[custObj addProducts:itemSet];
...

but this fails. In this try, I understand a product object hasn’t been explicitly created and, as such, an explicit product assignment hasn’t taken place but I thought, perhaps, the accessor would take care of that.

What, therefore, is the correct usage of addProducts, and for that matter, removeProducts?

Thanks.

  • 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-06-05T23:23:04+00:00Added an answer on June 5, 2026 at 11:23 pm

    The set you are passing to addProducts contains NSString, not Products.

    NSMutableSet* products = [NSMutableSet set];
    
    Products* product = [NSEntityDescription insertNewObjectForEntityForName: @"Products" inManagedObjectContext: context];
    product.item = @"Widget 1";
    [products addObject: product];
    
    product = [NSEntityDescription insertNewObjectForEntityForName: @"Products" inManagedObjectContext: context];
    product.item = @"Widget 2";
    [products addObject: product];
    
    [customer addProducts: products];
    

    Per se, the accessor have no way to know what the strings you gave it in first place were for. You have to provide a set containing the right kind of entities.

    That said, you can define your own accessor, taking a set of strings and inserting properly initialized Products in your relationship : addProductsWithStrings: per example.

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

Sidebar

Related Questions

I have a pre-existing project to which I've added Core Data models. I added
I'm following this Core Data tutorial and one thing is confusing me. Let's say
I have an entity in my core data model like this: @interface Selection :
I'm trying to port over some Core Data code over from a test Mac
I have an Entity Item in Core Data. Item has two attributes: name and
I need to map this data into one Core Data object (EventList). { eventList:[
On this test page: http:// www.onebagoneearth.com/ Products-test , where it says oboe love series,
I have this test slider http://jsfiddle.net/dUyLY/2/ In Chrome it works nice, but in firefox
I have this test [Test] public void SaveInventoryItemLoad_Will_Call_WCF_Service_SaveInventoryItemLoad() { adapter.SaveInventoryItemLoad(new List<InventoryItemLoadProxy>()); itemMasterBusinessClientMock.Verify(x => x.SaveInventoryItemLoad(It.IsAny<List<InventoryItemLoadProxy>>()),
I have this test case def setUp(self): self.user = User.objects.create(username=tauri, password='gaul') def test_loginin_student_control_panel(self): c

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.