title: +entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Account''
body: <p>I have tried a lot of options, but can't find the solution for this problem. I created a Core Data file and named the entity Account, Created an string attribute called username. Then edited the class of the entity to NSManagedObject, not sure if this is right. Now, the following code is in my LoginViewController:</p>
  
  <pre><code>- (void)viewDidLoad
  {
      [super viewDidLoad];
  
      ITAppDelegate *appDelegate = (ITAppDelegate *)[[UIApplication sharedApplication] delegate];
      NSManagedObjectContext *context = appDelegate.managedObjectContext;
  
      Account *newAccount = [NSEntityDescription insertNewObjectForEntityForName:@"Account" inManagedObjectContext:context];
      [newAccount setValue:@"Jorge" forKey:@"username"];
      [newAccount setPassword:@"password"];
  
      NSLog(@"username:%@   password: %@", [newAccount username], [newAccount password]);
  
  }
  </code></pre>
  
  <p>I followed <a href="http://josebolanos.wordpress.com/2012/03/16/core-data-passwords-in-keychain/" rel="noreferrer">This Tutorial</a> and my code files look like this:</p>
  
  <p>ITAppDelegate.h</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  
  @interface ITAppDelegate : UIResponder &lt;UIApplicationDelegate&gt;
  
  @property (strong, nonatomic) UIWindow *window;
  
  @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
  @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
  @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
  
  @end
  </code></pre>
  
  <p>ITAppDelegate.m</p>
  
  <pre><code>#import "ITAppDelegate.h"
  #import "LoginViewController.h"
  
  @implementation ITAppDelegate
  
  @synthesize managedObjectContext = _managedObjectContext;
  @synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
  @synthesize managedObjectModel = _managedObjectModel;
  
  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary    *)launchOptions
  {
      // Override point for customization after application launch.
      return YES;
  }
  
  #pragma mark - Core Data stack
  
  - (NSManagedObjectContext *)managedObjectContext
  {
  if (_managedObjectContext != nil)
  {
      return _managedObjectContext;
  }
  
  NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
  if (coordinator != nil)
  {
      _managedObjectContext = [[NSManagedObjectContext alloc] init];
      [_managedObjectContext  setPersistentStoreCoordinator:coordinator];
  }
  return _managedObjectContext;
  }
  
  
  - (NSManagedObjectModel *)managedObjectModel
  {
  if (_managedObjectModel != nil)
  {
      return _managedObjectModel;
  }
  NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"];
  _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
  return _managedObjectModel;
  }
  
  
  - (NSPersistentStoreCoordinator *)persistentStoreCoordinator
  {
  if (_persistentStoreCoordinator != nil)
  {
      return _persistentStoreCoordinator;
  }
  
      return _persistentStoreCoordinator;
  }
  
  @end
  </code></pre>
  
  <p>AccountBase.h</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  #import &lt;CoreData/CoreData.h&gt;
  
  @interface AccountBase : NSManagedObject
  
  @property (nonatomic, retain) NSString *username;
  
  
  @end
  </code></pre>
  
  <p>AccountBase.m</p>
  
  <pre><code>#import "AccountBase.h"
  
  @implementation AccountBase
  
  @dynamic username;
  
  @end
  </code></pre>
  
  <p>Account.h</p>
  
  <pre><code>#import "AccountBase.h"
  #import &lt;CoreData/CoreData.h&gt;
  
  @interface Account : AccountBase
  
  @property (nonatomic, assign) NSString *password;
  
  @end
  </code></pre>
  
  <p>Account.m</p>
  
  <pre><code>#import "Account.h"
  #import "KeychainHelper.h"
  
  @implementation Account
  
  - (NSString*)password
  {
      if (self.username)
          return [KeychainHelper getPasswordForKey:self.username];
      return nil;
  }
  
  - (void)setPassword:(NSString*)aPassword
  {
      if (self.username)
          [KeychainHelper setPassword:aPassword forKey:self.username];
  }
  
  - (void)prepareForDeletion
  {
      if (self.username)
          [KeychainHelper removePasswordForKey:self.username];
  }
  @end
  </code></pre>
  
  <p>KeychainHelper.h</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  
  @interface KeychainHelper : NSObject
  
  + (NSString*)getPasswordForKey:(NSString*)aKey;
  + (void)setPassword:(NSString*)aPassword forKey:(NSString*)aKey;
  + (void)removePasswordForKey:(NSString*)aKey;
  
  @end
  </code></pre>
  
  <p>KeychainHelper.m</p>
  
  <pre><code>#import "KeychainHelper.h"
  #import &lt;Security/Security.h&gt;
  
  @interface KeychainHelper ()
  + (NSMutableDictionary*)dictionaryForKey:(NSString*)aKey;
  @end
  
  @implementation KeychainHelper
  
  
  static const NSString *ironTrainers = @"com.domain.myapplication";
  
  + (NSMutableDictionary*)dictionaryForKey:(NSString*)aKey
  {
      NSData *encodedKey = [aKey dataUsingEncoding:NSUTF8StringEncoding];
  
      NSMutableDictionary *searchDictionary = [NSMutableDictionary dictionary];
  
      [searchDictionary setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id)kSecClass];
      [searchDictionary setObject:encodedKey forKey:(__bridge id)kSecAttrGeneric];
      [searchDictionary setObject:encodedKey forKey:(__bridge id)kSecAttrAccount];
      [searchDictionary setObject:ironTrainers forKey:(__bridge id)kSecAttrService];
  
      return searchDictionary;
  }  
  
  + (NSString*)getPasswordForKey:(NSString*)aKey
  {
      NSString *password = nil;
  
      NSMutableDictionary *searchDictionary = [self dictionaryForKey:aKey];
      [searchDictionary setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];
      [searchDictionary setObject:(id)kCFBooleanTrue forKey:(__bridge id)kSecReturnData];
  
      CFTypeRef result = NULL;
      BOOL statusCode = SecItemCopyMatching((__bridge CFDictionaryRef)searchDictionary, &amp;result);
      if (statusCode == errSecSuccess) {
          NSData *resultData = CFBridgingRelease(result);
          password = [[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];
      }
      return (__bridge NSString *)(result);
  }
  
  + (void)removePasswordForKey:(NSString*)aKey
  {
      NSMutableDictionary *keyDictionary = [self dictionaryForKey:aKey];
      SecItemDelete((__bridge CFDictionaryRef)keyDictionary);
   }
  
  + (void)setPassword:(NSString*)aPassword forKey:(NSString*)aKey
  {
      [KeychainHelper removePasswordForKey:aKey];
  
      NSData *encodedPassword = [aPassword dataUsingEncoding:NSUTF8StringEncoding];
  
      NSMutableDictionary *keyDictionary = [self dictionaryForKey:aKey];
      [keyDictionary setObject:encodedPassword forKey:(__bridge id)kSecValueData];
      SecItemAdd((__bridge CFDictionaryRef)keyDictionary, nil);
  }
  
  @end
  </code></pre>
  
  <p>Any help appreciated. Thanks.</p>