本文介绍了+ entityForName:nil不是合法的NSManagedObjectContext参数,用于搜索实体名称“Account”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了很多选项,但找不到这个问题的解决方案。我创建了一个Core Data文件并命名为实体Account,创建了一个名为username的字符串属性。然后编辑实体的类NSManagedObject,不知道这是否正确。现在,以下代码在我的LoginViewController中:

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:

- (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]);

}

我跟着和我的代码文件看起来像这样:

I followed This Tutorial and my code files look like this:

ITAppDelegate.h

ITAppDelegate.h

#import <UIKit/UIKit.h>

@interface ITAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

@end

ITAppDelegate.m

ITAppDelegate.m

#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

AccountBase.h

AccountBase.h

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>

@interface AccountBase : NSManagedObject

@property (nonatomic, retain) NSString *username;


@end

AccountBase.m

AccountBase.m

#import "AccountBase.h"

@implementation AccountBase

@dynamic username;

@end

Account.h

Account.h

#import "AccountBase.h"
#import <CoreData/CoreData.h>

@interface Account : AccountBase

@property (nonatomic, assign) NSString *password;

@end

Account.m

Account.m

#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

KeychainHelper.h

KeychainHelper.h

#import <Foundation/Foundation.h>

@interface KeychainHelper : NSObject

+ (NSString*)getPasswordForKey:(NSString*)aKey;
+ (void)setPassword:(NSString*)aPassword forKey:(NSString*)aKey;
+ (void)removePasswordForKey:(NSString*)aKey;

@end

KeychainHelper.m

KeychainHelper.m

#import "KeychainHelper.h"
#import <Security/Security.h>

@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, &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

感谢。

推荐答案

- (NSManagedObjectContext *)managedObjectContext
{
    if (managedObjectContext != nil) return managedObjectContext;

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {

        managedObjectContext = [[NSManagedObjectContext alloc] init];
        [managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return managedObjectContext;
}




  • 您尚未提供延迟加载实现 persistentStoreCoordinator

  • 因此 coordinator 始终为 nil

  • ,因此您将始终从此方法中返回 nil

  • 这表示您将始终遇到上述错误。

    • You haven't provided a lazy loading implementation of persistentStoreCoordinator
    • so coordinator will always be nil
    • so you will always be returning nil from this method
    • which means you will always get the error above.
    • 解释错误:

      从阅读它并不立即显而易见,但这意味着 nil 不是合法的东西传递的托管对象上下文。一读时,看起来你正在做 entityForName:nil ,但事实并非如此。

      It's not immediately obvious from reading it, but this means that nil is not a legal thing to pass for the managed object context. On first reading, it looks like you're doing entityForName:nil but that isn't the case.

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

      To fix the problem, you will need to provide a valid persistent store coordinator. I have a small article here which explains just how little code you need to set up a core data stack, this may help you.

      这篇关于+ entityForName:nil不是合法的NSManagedObjectContext参数,用于搜索实体名称“Account”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 23:57