iphone coredata setValue 在 if 语句中不起作用
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
[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.