title: Core Data Relationships: How to insert a new object into an entity and create a relationship to an existing object in another entity
body: <p>I am building a little iPhone app which allows users to keep score of games they may be playing with friends. I now need to use relationships in Core Data but can't quite seem to get it working. </p>
  
  <p><strong>I want to be able to add new data into one entity while creating a relationship to existing data in a different entity. How can I achieve this?</strong></p>
  
  <p><em>Please note I am new to Core Data and have spent the best part of today trying to figure this out but have run out of luck. Any help would be very much appreciated.</em></p>
  
  <hr>
  
  <p>I have 3 entities: <strong>Scores</strong>, <strong>Games</strong> and <strong>Players</strong>.</p>
  
  <p><strong>Scores</strong> Attributes: <code>date</code>, <code>player1Score</code>, <code>player2Score</code> and <code>status</code>.</p>
  
  <p><strong>Games</strong> Attributes: <code>title</code>.</p>
  
  <p><strong>Players</strong> Attributes: <code>name</code>.</p>
  
  <p>I have many to many relationships between (<strong>Scores</strong> &lt;&lt;--->> <strong>Games</strong>) and (<strong>Scores</strong> &lt;&lt;--->> <strong>Players</strong>)</p>
  
  <hr>
  
  <p>I already have a list of both games and players. The user selects which game and who is playing and with this information a set of objects is created in the <strong>Scores</strong> entity with relationships to the chosen game and players. </p>
  
  <p>Here is my source:</p>
  
  <pre><code>//  Scores.h
  
  #import &lt;Foundation/Foundation.h&gt;
  #import &lt;CoreData/CoreData.h&gt;
  
  @class Games, Players;
  
  @interface Scores : NSManagedObject
  
  @property (nonatomic, retain) NSDate * date;
  @property (nonatomic, retain) NSNumber * player1Score;
  @property (nonatomic, retain) NSNumber * player2Score;
  @property (nonatomic, retain) NSNumber * status;
  @property (nonatomic, retain) NSSet *game;
  @property (nonatomic, retain) NSSet *player;
  @end
  
  @interface Scores (CoreDataGeneratedAccessors)
  
  - (void)addGameObject:(Games *)value;
  - (void)removeGameObject:(Games *)value;
  - (void)addGame:(NSSet *)values;
  - (void)removeGame:(NSSet *)values;
  
  - (void)addPlayerObject:(Players *)value;
  - (void)removePlayerObject:(Players *)value;
  - (void)addPlayer:(NSSet *)values;
  - (void)removePlayer:(NSSet *)values;
  
  @end
  </code></pre>
  
  <hr>
  
  <pre><code>// SC_ScoreViewController.h
  
  #import &lt;UIKit/UIKit.h&gt;
  
  @interface SC_ScoreViewController : UIViewController
  
  @property (strong) NSIndexPath *game;
  @property (strong) NSIndexPath *playerOne;
  @property (strong) NSIndexPath *playerTwo;
  
  @property (weak, nonatomic) IBOutlet UILabel *playerOneName;
  @property (weak, nonatomic) IBOutlet UILabel *playerTwoName;
  
  @end
  </code></pre>
  
  <hr>
  
  <pre><code>//  SC_ScoreViewController.m
  
  #import "SC_ScoreViewController.h"
  #import "Scores.h"
  #import "Players.h"
  #import "Games.h"
  
  @interface SC_ScoreViewController ()
  
  @end
  
  @implementation SC_ScoreViewController
  
  @synthesize game;
  @synthesize playerOne;
  @synthesize playerTwo;
  
  // managedObjectContext (context)
  - (NSManagedObjectContext *)managedObjectContext {
      NSManagedObjectContext *context = nil;
      id delegate = [[UIApplication sharedApplication] delegate];
  if ([delegate respondsToSelector:@selector(managedObjectContext)]) {
          context = [delegate managedObjectContext];
      }
      return context;
  }
  
  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  {
      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
      if (self) {
          // Custom initialization
      }
      return self;
  }
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
  
      // Setup Nav Title
      self.navigationItem.title = [self.game valueForKey:@"title"];
  
      // Setup Player's Names
      [self.playerOneName setText:[self.playerOne valueForKey:@"name"]];
      [self.playerTwoName setText:[self.playerTwo valueForKey:@"name"]];
  
  
      Scores * newEntry = [NSEntityDescription insertNewObjectForEntityForName:@"Scores"        inManagedObjectContext:self.managedObjectContext];
      newEntry.player1Score = 0;
      newEntry.player2Score = 0;
      newEntry.status = nil;
      newEntry.player = self.playerOne; // Incompatible pointer types assigning to NSSet from NSIndexPath
  
      NSError *error;
      if (![self.managedObjectContext save:&amp;error]) {
          NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
      }
  
      - (void)didReceiveMemoryWarning
      {
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
      }
  
      @end
  </code></pre>
  
  <hr>
  
  <p>I hope I have been clear and provided enough information. This is my first question so I hope all is in order. Any help would be amazing, thank you.</p>