首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我尝试将我的字段存储在它无法存储的核心数据中

我尝试将我的字段存储在它无法存储的核心数据中
EN

Stack Overflow用户
提问于 2016-02-04 01:17:12
回答 1查看 61关注 0票数 0

在我的代码中,我使用了简单的注册页面。这些字段包括名字、姓氏、电子邮件、密码和电话号码。在这种情况下,如果我注册了一个新的成员,意味着新给定的电子邮件id已经存储了,这意味着它不能存储在核心数据中。这就是我的情况。

这是我的代码,

代码语言:javascript
运行
复制
//Appdelegate call

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];  

NSManagedObjectContext *context = [appDelegate managedObjectContext];

//Fetch Request

NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Device"
                                              inManagedObjectContext:context];

NSFetchRequest *request = [[NSFetchRequest alloc]init];

[request setEntity:entityDesc];

NSLog(@"Your Request is : %@",request);

NSPredicate *pred = [NSPredicate predicateWithFormat:@"(email = %@)", email];

[request setPredicate:pred];

NSError *error=nil;
NSArray *result = [context executeFetchRequest:request error:&error];

NSLog(@"Your Result is: %@",result);


if ([result count] > 0){

    if ([email isEqual:[[result valueForKey:@"email"]lastObject]])
    {
        [alertView AlreadyRegisterAlertView];
    }
    else{

        NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Device" inManagedObjectContext:context];

        NSLog(@"Welcome for Register Section");

        [newDevice setValue:nameFile.fname forKey:@"fname"];
        [newDevice setValue:nameFile.surname forKey:@"sname"];
        [newDevice setValue:nameFile.eid forKey:@"email"];
        [newDevice setValue:nameFile.phone forKey:@"phone"];
        [newDevice setValue:nameFile.passd forKey:@"password"];
        [newDevice setValue:nameFile.imgProfile forKey:@"photo"];


        [alertView RegistrationSucessAlertView];

        NSLog(@"Your First Name is: %@",nameFile.fname);
        NSLog(@"Your Sur-Name is: %@",nameFile.surname);
        NSLog(@"Your Email id is: %@",nameFile.eid);
        NSLog(@"Your Phone Number is: %@",nameFile.phone);
        NSLog(@"Your Password is: %@",nameFile.passd);
        NSLog(@"Your Profile Image is: %@", nameFile.imgProfile);
    }

}

// Save the object to persistent store

if (![context save:&error]) {

    NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}

在这段代码中,条件if([result count]>0)为false,这意味着我可以做什么。

EN

回答 1

Stack Overflow用户

发布于 2016-02-05 01:03:36

我看到的问题就在这条线上

NSPredicate *pred = [NSPredicate predicateWithFormat:@"(email = %@)", email];

代码应该是

NSPredicate *pred = [NSPredicate predicateWithFormat:@"email == %@", email];

更新:

首先,我认为逻辑是不正确的。在实际保存之前,您正在尝试获取核心数据对象。您必须插入新实体并将其保存到核心数据。然后,如果需要,您必须获取该对象以进行任何进一步处理。

这就是为什么你的if条件每次都会失败。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35183555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档