忽略“无法调用 NSManagedObject 类上的指定初始值设定项”
核心数据库跟踪用户信息。目标是使用核心数据登录,足够简单。 模拟应用程序并完美登录。 Xcode 不显示任何错误或警告。
控制台输出显示: 无法调用 NSManagedObject 类“登录”上的指定初始值设定项 我可以忽略这个输出吗?
Login.h 和 Login.m 是由 Xcode 本身根据数据模型创建的。
登录.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class User;
@interface Login : NSManagedObject {
@private
}
@property (nonatomic, retain) NSString * password;
@property (nonatomic, retain) User *user;
+ (User *)loginWithEmail:(NSString *)email withPassword:(NSString *)password inManagedObjectContext:(NSManagedObjectContext *)context;
@end
登录.m
#import "Login.h"
#import "User.h"
@interface Login ()
- (User *)isValidEmail:(NSString *)email inManagedObjectContext:(NSManagedObjectContext *)context;
@end
@implementation Login
@dynamic password;
@dynamic user;
- (User *)isValidEmail:(NSString *)email inManagedObjectContext:(NSManagedObjectContext *)context
{
User *user = nil;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:context];
request.predicate = [NSPredicate predicateWithFormat:@"email = %@", email];
NSError *error = nil;
user = [[context executeFetchRequest:request error:&error] lastObject];
[request release];
return user;
}
+ (User *)loginWithEmail:(NSString *)email withPassword:(NSString *)password inManagedObjectContext:(NSManagedObjectContext *)context
{
Login *loginHelper = [[Login alloc] init];
User *user = nil;
if ((user = [loginHelper isValidEmail:email inManagedObjectContext:context])) {
if ([user.login.password isEqualToString:password]) {
// correct login
} else {
// invalid password
user = nil;
}
} else {
// user does not exist
user = nil;
}
[loginHelper release];
return user;
}
@end
A core database keeps track of user info. The goal is to login using the core data, simple enough.
Simulating the app and logging in works perfectly.
Xcode does not show any errors or warnings.
Console output shows:
Failed to call designated initializer on NSManagedObject class 'Login'
Can I ignore this output ??
Login.h and Login.m are created by Xcode itself from the data model.
Login.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class User;
@interface Login : NSManagedObject {
@private
}
@property (nonatomic, retain) NSString * password;
@property (nonatomic, retain) User *user;
+ (User *)loginWithEmail:(NSString *)email withPassword:(NSString *)password inManagedObjectContext:(NSManagedObjectContext *)context;
@end
Login.m
#import "Login.h"
#import "User.h"
@interface Login ()
- (User *)isValidEmail:(NSString *)email inManagedObjectContext:(NSManagedObjectContext *)context;
@end
@implementation Login
@dynamic password;
@dynamic user;
- (User *)isValidEmail:(NSString *)email inManagedObjectContext:(NSManagedObjectContext *)context
{
User *user = nil;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:context];
request.predicate = [NSPredicate predicateWithFormat:@"email = %@", email];
NSError *error = nil;
user = [[context executeFetchRequest:request error:&error] lastObject];
[request release];
return user;
}
+ (User *)loginWithEmail:(NSString *)email withPassword:(NSString *)password inManagedObjectContext:(NSManagedObjectContext *)context
{
Login *loginHelper = [[Login alloc] init];
User *user = nil;
if ((user = [loginHelper isValidEmail:email inManagedObjectContext:context])) {
if ([user.login.password isEqualToString:password]) {
// correct login
} else {
// invalid password
user = nil;
}
} else {
// user does not exist
user = nil;
}
[loginHelper release];
return user;
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的理解是,您通常不会显式分配/初始化 NSManagedObjects 或 NSManagedObjects 的子类 - CoreData 根据需要处理实例化和取消分配托管对象 - 但您尝试在 loginWithEmail 方法中分配/初始化您自己的子类。所以这可能就是您收到错误的原因。
从更广泛的意义上来说,这种实现似乎模糊了应该是现成的数据模型(您的 NSManagedObject 子类)和“登录”的应用程序逻辑之间的界限 - 所以我建议重新考虑您的架构位更坚定地反映模型-视图-控制器原则!快乐编码。
My understanding is that you typically don't alloc/init NSManagedObjects or subclasses of NSManagedObjects explicitly- CoreData handles instantiating and deallocating managed objects as necessary - but you are attempting to alloc/init your own subclass in your loginWithEmail method. So that's probably why you're getting the error.
In the broader sense, this implementation seems to blur the lines between what should be a cut-and-dry data model (your NSManagedObject subclass), and the application logic of "logging in" - so I'd recommend reconsidering your architecture just a bit to more firmly reflect model-view-controller principles! Happy coding.