ios – entityForName:nil不是合法的NSManagedObjectContext参数,用于搜索实体名称“Account”

前端之家收集整理的这篇文章主要介绍了ios – entityForName:nil不是合法的NSManagedObjectContext参数,用于搜索实体名称“Account”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经尝试了很多选项,但找不到此问题的解决方案.我创建了一个Core Data文件,并命名为实体Account,创建一个名为username的字符串属性.然后将实体的类编辑为NSManagedObject,不知道这是否正确.现在,我的LoginViewController中有以下代码
  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4.  
  5. ITAppDelegate *appDelegate = (ITAppDelegate *)[[UIApplication sharedApplication] delegate];
  6. NSManagedObjectContext *context = appDelegate.managedObjectContext;
  7.  
  8. Account *newAccount = [NSEntityDescription insertNewObjectForEntityForName:@"Account" inManagedObjectContext:context];
  9. [newAccount setValue:@"Jorge" forKey:@"username"];
  10. [newAccount setPassword:@"password"];
  11.  
  12. NSLog(@"username:%@ password: %@",[newAccount username],[newAccount password]);
  13.  
  14. }

我跟着This Tutorial,我的代码文件如下所示:

ITAppDelegate.h

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface ITAppDelegate : UIResponder <UIApplicationDelegate>
  4.  
  5. @property (strong,nonatomic) UIWindow *window;
  6.  
  7. @property (readonly,strong,nonatomic) NSManagedObjectContext *managedObjectContext;
  8. @property (readonly,nonatomic) NSManagedObjectModel *managedObjectModel;
  9. @property (readonly,nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
  10.  
  11. @end

ITAppDelegate.m

  1. #import "ITAppDelegate.h"
  2. #import "LoginViewController.h"
  3.  
  4. @implementation ITAppDelegate
  5.  
  6. @synthesize managedObjectContext = _managedObjectContext;
  7. @synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
  8. @synthesize managedObjectModel = _managedObjectModel;
  9.  
  10. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  11. {
  12. // Override point for customization after application launch.
  13. return YES;
  14. }
  15.  
  16. #pragma mark - Core Data stack
  17.  
  18. - (NSManagedObjectContext *)managedObjectContext
  19. {
  20. if (_managedObjectContext != nil)
  21. {
  22. return _managedObjectContext;
  23. }
  24.  
  25. NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
  26. if (coordinator != nil)
  27. {
  28. _managedObjectContext = [[NSManagedObjectContext alloc] init];
  29. [_managedObjectContext setPersistentStoreCoordinator:coordinator];
  30. }
  31. return _managedObjectContext;
  32. }
  33.  
  34.  
  35. - (NSManagedObjectModel *)managedObjectModel
  36. {
  37. if (_managedObjectModel != nil)
  38. {
  39. return _managedObjectModel;
  40. }
  41. NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"];
  42. _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
  43. return _managedObjectModel;
  44. }
  45.  
  46.  
  47. - (NSPersistentStoreCoordinator *)persistentStoreCoordinator
  48. {
  49. if (_persistentStoreCoordinator != nil)
  50. {
  51. return _persistentStoreCoordinator;
  52. }
  53.  
  54. return _persistentStoreCoordinator;
  55. }
  56.  
  57. @end

AccountBase.h

  1. #import <UIKit/UIKit.h>
  2. #import <CoreData/CoreData.h>
  3.  
  4. @interface AccountBase : NSManagedObject
  5.  
  6. @property (nonatomic,retain) NSString *username;
  7.  
  8.  
  9. @end

AccountBase.m

  1. #import "AccountBase.h"
  2.  
  3. @implementation AccountBase
  4.  
  5. @dynamic username;
  6.  
  7. @end

Account.h

  1. #import "AccountBase.h"
  2. #import <CoreData/CoreData.h>
  3.  
  4. @interface Account : AccountBase
  5.  
  6. @property (nonatomic,assign) NSString *password;
  7.  
  8. @end

Account.m

  1. #import "Account.h"
  2. #import "KeychainHelper.h"
  3.  
  4. @implementation Account
  5.  
  6. - (NSString*)password
  7. {
  8. if (self.username)
  9. return [KeychainHelper getPasswordForKey:self.username];
  10. return nil;
  11. }
  12.  
  13. - (void)setPassword:(NSString*)aPassword
  14. {
  15. if (self.username)
  16. [KeychainHelper setPassword:aPassword forKey:self.username];
  17. }
  18.  
  19. - (void)prepareForDeletion
  20. {
  21. if (self.username)
  22. [KeychainHelper removePasswordForKey:self.username];
  23. }
  24. @end

KeychainHelper.h

  1. #import <Foundation/Foundation.h>
  2.  
  3. @interface KeychainHelper : NSObject
  4.  
  5. + (NSString*)getPasswordForKey:(NSString*)aKey;
  6. + (void)setPassword:(NSString*)aPassword forKey:(NSString*)aKey;
  7. + (void)removePasswordForKey:(NSString*)aKey;
  8.  
  9. @end

KeychainHelper.m

  1. #import "KeychainHelper.h"
  2. #import <Security/Security.h>
  3.  
  4. @interface KeychainHelper ()
  5. + (NSMutableDictionary*)dictionaryForKey:(NSString*)aKey;
  6. @end
  7.  
  8. @implementation KeychainHelper
  9.  
  10.  
  11. static const NSString *ironTrainers = @"com.domain.myapplication";
  12.  
  13. + (NSMutableDictionary*)dictionaryForKey:(NSString*)aKey
  14. {
  15. NSData *encodedKey = [aKey dataUsingEncoding:NSUTF8StringEncoding];
  16.  
  17. NSMutableDictionary *searchDictionary = [NSMutableDictionary dictionary];
  18.  
  19. [searchDictionary setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id)kSecClass];
  20. [searchDictionary setObject:encodedKey forKey:(__bridge id)kSecAttrGeneric];
  21. [searchDictionary setObject:encodedKey forKey:(__bridge id)kSecAttrAccount];
  22. [searchDictionary setObject:ironTrainers forKey:(__bridge id)kSecAttrService];
  23.  
  24. return searchDictionary;
  25. }
  26.  
  27. + (NSString*)getPasswordForKey:(NSString*)aKey
  28. {
  29. NSString *password = nil;
  30.  
  31. NSMutableDictionary *searchDictionary = [self dictionaryForKey:aKey];
  32. [searchDictionary setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];
  33. [searchDictionary setObject:(id)kcfBooleanTrue forKey:(__bridge id)kSecReturnData];
  34.  
  35. CFTypeRef result = NULL;
  36. BOOL statusCode = SecItemCopyMatching((__bridge CFDictionaryRef)searchDictionary,&result);
  37. if (statusCode == errSecSuccess) {
  38. NSData *resultData = CFBridgingRelease(result);
  39. password = [[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];
  40. }
  41. return (__bridge NSString *)(result);
  42. }
  43.  
  44. + (void)removePasswordForKey:(NSString*)aKey
  45. {
  46. NSMutableDictionary *keyDictionary = [self dictionaryForKey:aKey];
  47. SecItemDelete((__bridge CFDictionaryRef)keyDictionary);
  48. }
  49.  
  50. + (void)setPassword:(NSString*)aPassword forKey:(NSString*)aKey
  51. {
  52. [KeychainHelper removePasswordForKey:aKey];
  53.  
  54. NSData *encodedPassword = [aPassword dataUsingEncoding:NSUTF8StringEncoding];
  55.  
  56. NSMutableDictionary *keyDictionary = [self dictionaryForKey:aKey];
  57. [keyDictionary setObject:encodedPassword forKey:(__bridge id)kSecValueData];
  58. SecItemAdd((__bridge CFDictionaryRef)keyDictionary,nil);
  59. }
  60.  
  61. @end

任何帮助赞赏.谢谢.

解决方法

  1. - (NSManagedObjectContext *)managedObjectContext
  2. {
  3. if (managedObjectContext != nil) return managedObjectContext;
  4.  
  5. NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
  6. if (coordinator != nil) {
  7.  
  8. managedObjectContext = [[NSManagedObjectContext alloc] init];
  9. [managedObjectContext setPersistentStoreCoordinator:coordinator];
  10. }
  11. return managedObjectContext;
  12. }

>你没有提供一个延迟加载的persistentStoreCoordinator实现
>所以协调员总是没有
所以你总是从这个方法返回零
这意味着你会总是得到上面的错误.

解释错误

+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name ‘Account’

阅读它不是很明显,但这意味着,对于托管对象上下文来说,这不是一个合法的东西.一读,看起来你正在做entityForName:nil,但事实并非如此.

解决此问题,您需要提供一个有效的持久性存储协调器.我有一篇小文章@L_403_1@,它解释了你需要多少代码来设置核心数据堆栈,这可能会帮助你.

猜你在找的iOS相关文章