title: Adding a new record programmatically to a Cocoa Core Data Entity
body: <p>i'm making a simple document-based application in Cocoa. Each document of this app should basically manage an array of Dates and Notes, so each record is a date and a note (textview). Also each document is protected by password.</p>
  
  <p>To do that i created a Core Data entity called HistoryElement (that contains a date and a notes attribute), i also created a Settings entity that should have only a record which contains the password to open the file (i didn't found a better method, there is one ? The password is tied to each file, so i can't use preferences because it's not a global application password).</p>
  
  <p>I have a preference tab which contain a Password textfield that binds to the password attribute of the Settings entity.</p>
  
  <p>Ok...the problem is now this: when i create a new document there are no records on the Settings entity, so i wish to programmatically add one, so the user can put (if it want to protect it's file) the password in the password text field.</p>
  
  <p>Instead, if i'm opening an existing file it should find that a record for Settings entity has been already add and it shouldn't create it again, instead the password text field should use this one.</p>
  
  <p>I tried many ways, but i'm unable to do that. I tried for example this:</p>
  
  <pre><code>if([[settingsArrayController arrangedObjects] count] == 0) {`
  
      NSLog(@"Init settings");`
  
      [settingsArrayController add:self];`
  
  } 
  </code></pre>
  
  <p>It seems that it adds a new record when i create a new document, but if i put a password in the password text field and then save the document, when i open the document again the <code>[[settingsArrayController arrangedObjects] count]</code> returns <strong><code>0</code></strong> and it creates a new record again...</p>
  
  <p>How can i do that ? There is a better/simple/elegant way to protect a document with a password ?</p>