iphone coredata setValue 在 if 语句中不起作用

发布于 2024-11-07 08:21:32 字数 1206 浏览 1 评论 0原文

我的 coredata 工作正常,但是当我将 setValue 放入 if 中时,它不起作用,

这是代码>

-(void) salvalo {

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

NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newDay;
newDay = [NSEntityDescription insertNewObjectForEntityForName:@"DaysAttendance"
                                       inManagedObjectContext:context];

[newDay setValue:startday forKey:@"start"];
[newDay setValue:today forKey:@"datef"];
[newDay setValue:myNumber forKey:@"empID"]; 
[newDay setValue:myNumber2 forKey:@"intID"];
//[newDay setValue:internet forKey:@"sinch"];


NSError *error;
[context save:&error];

if ([internet isEqualToString:@"YES"] ) {
    [self insertJson];

    [newDay setValue:internet forKey:@"sinch"];

}
else {
    [newDay setValue:@"NO" forKey:@"sinch"];

}



//[self.view removeFromSuperview];
[self dismissModalViewControllerAnimated:YES];


      }

请注意,当我在注释部分使用 if 内的代码时

//[newDay setValue:internet forKey:@"sinch"];

,它通常将该值(是/否)保存到 coredata,但是在 if 内部,它不起作用,即使我将所有委托和上下文内容放入 if 中,

那么问题是什么?如何修复它?

多谢!

I have my coredata working fine, but when I put a setValue inside an if, it doesn't work,

this is the code>

-(void) salvalo {

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

NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newDay;
newDay = [NSEntityDescription insertNewObjectForEntityForName:@"DaysAttendance"
                                       inManagedObjectContext:context];

[newDay setValue:startday forKey:@"start"];
[newDay setValue:today forKey:@"datef"];
[newDay setValue:myNumber forKey:@"empID"]; 
[newDay setValue:myNumber2 forKey:@"intID"];
//[newDay setValue:internet forKey:@"sinch"];


NSError *error;
[context save:&error];

if ([internet isEqualToString:@"YES"] ) {
    [self insertJson];

    [newDay setValue:internet forKey:@"sinch"];

}
else {
    [newDay setValue:@"NO" forKey:@"sinch"];

}



//[self.view removeFromSuperview];
[self dismissModalViewControllerAnimated:YES];


      }

please note that when I use the code inside the if in the commented part

//[newDay setValue:internet forKey:@"sinch"];

, it saves normally that value (YES/NO) to the coredata, but inside the if, it doesnt work, even if I put al the delegate and context stuff inside the if,,

so what is the problem? how to fix it?

thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑饮青盏花 2024-11-14 08:21:32

[context save:&error]; 行位于 if 语句之前。

您可能正确设置了新值,只是没有保存。问题可能就出在这个疏忽上。

The line [context save:&error]; is before the if statements.

You may be setting the new value properly, just not saving. The problem might just be this oversight.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文