所以我正在以社交媒体应用程序的形式创建一个应用程序。我正在使用此处找到的教程作为跳板,因为我仍在尝试将我的大脑包裹在 Core Data 周围。我偏离了本教程,添加了一个注册按钮,该按钮将用户带到一个新的视图控制器并创建了一个 .h 和一个 .m 文件,并将新成员屏幕设置为引用 .h 和 .m 文件。.h 的设置如下:
#import <UIKit/UIKit.h>
@interface NewMemberViewController : UIViewController
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (strong, nonatomic) IBOutlet UITextField *nameTF;
@property (strong, nonatomic) IBOutlet UITextField *ageTF;
@property (strong, nonatomic) IBOutlet UITextField *usernameTF;
@property (strong, nonatomic) IBOutlet UITextField *passwordTF;
- (IBAction)alreadyMember:(id)sender;
- (IBAction)checkAndLogin:(id)sender;
@end
对于.m:
#import "NewMemberViewController.h"
#import "CoreDataHelper.h"
@interface NewMemberViewController ()
@end
@implementation NewMemberViewController
@synthesize usernameTF, ageTF, passwordTF, nameTF, managedObjectContext;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//If the user is already a member simply dismiss the VC
- (IBAction)alreadyMember:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
//When done editing keyboard
- (IBAction)checkAndLogin:(id)sender {
managedObjectContext =self.managedObjectContext;
//Textfield Reference
UITextField *tf = (UITextField *)sender;
//Check tag numbers If its equal to 1 or 2(nameTF or ageTF) then
if (tf.tag==1||tf.tag==2)
{
[sender resignFirstResponder];
NSLog(@"This is working");
}
//If its equal to 3 then this means the username text field is active
else if (tf.tag == 3)
{
[sender resignFirstResponder];
//do a quick search to see if username is availible
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(username == %@)", [usernameTF text]];
//Run the query to check if user exists
if([CoreDataHelper countForEntity:@"Users" withPredicate:pred andContext:managedObjectContext] > 0)
{
//we found a user
NSLog(@"oh no...");
}
}
}
@end
话虽如此,如果我运行我的应用程序并转到注册屏幕并测试用户是否存在,通过输入 admin 因为它已经存在,我在 Xcode 中收到以下错误:
Canvases[779:11603] * WebKit 在 webView 中丢弃了一个未捕获的异常:shouldInsertText:replacingDOMRange:givenAction: delegate: +entityForName: nil 不是搜索实体名称“用户”的合法 NSManagedObjectContext 参数
这是什么意思,为什么会这样?