[英]NSManagedObject's values suddenly going null
I am having issues with my application. 我的申请有问题。 In the viewDidLoad
method the _recipe
object will print out its values. 在viewDidLoad
方法中, _recipe
对象将打印出其值。 However when the addToFavourites
method is called the values of the _recipe
object return null and I can't figure out why any help will be majorly appreciated. 但是,当调用addToFavourites
方法时, _recipe
对象的值将返回null,我无法弄清楚为什么将对任何帮助大加赞赏。
Heres the class. 继承人班。
#import "DetailViewController.h"
#import "Recipe.h"
#import "AppDelegate.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
- (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.
self.recipetitle.text = _recipe.title;
self.recipeingrediants.text = _recipe.ingrediants;
self.recipemethod.text = _recipe.method;
NSString *str = self.recipe.img64;
NSData *data = [[NSData alloc] initWithBase64EncodedString:str options:0];
self.recipeimage.image = [UIImage imageWithData:data];
NSLog(@"This is odd.. %@",_recipe.title);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)addToFavourites:(id)sender {
NSManagedObjectContext *context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
Recipe *favourite = [NSEntityDescription insertNewObjectForEntityForName:@"Recipe" inManagedObjectContext:context];
[favourite setValue:self.recipe.title forKey:@"title"];
[favourite setValue:_recipe.ingrediants forKey:@"ingrediants"];
NSLog(@"ERMM WHAT %@",_recipe.title);
[favourite setValue:_recipe.method forKey:@"method"];
[favourite setValue:_recipe.img64 forKey:@"img64"];
[favourite setValue:_recipe.type forKey:@"type"];
NSError *err;
[context save:&err];
NSLog(@"SAVE FIRED");
}
@end
Ok I've figured it out! 好的,我知道了! The reference to the _recipe
was weak
! 对_recipe
的引用weak
! Therefore when the viewDidLoad
method ended the previous view got put on the heap. 因此,当viewDidLoad
方法结束时,先前的视图将放入堆中。 Due to the _recipe
not having an extra reference count it too went on the heap! 由于_recipe
没有额外的引用计数,因此它也进入了堆!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.