小编fer*_*ojr的帖子

+ entityForName:nil不是合法的NSManagedObjectContext参数,用于搜索实体名称"Account"

我尝试了很多选项,但找不到解决这个问题的方法.我创建了一个Core Data文件并命名了实体Account,创建了一个名为username的字符串属性.然后将实体的类编辑为NSManagedObject,不确定这是否正确.现在,以下代码在我的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]);

}
Run Code Online (Sandbox Code Playgroud)

我按照本教程,我的代码文件如下所示:

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
Run Code Online (Sandbox Code Playgroud)

ITAppDelegate.m

#import "ITAppDelegate.h"
#import "LoginViewController.h"

@implementation ITAppDelegate

@synthesize …
Run Code Online (Sandbox Code Playgroud)

login core-data keychain ios xcode5

19
推荐指数
1
解决办法
3万
查看次数

标签 统计

core-data ×1

ios ×1

keychain ×1

login ×1

xcode5 ×1