【问题标题】:NSManagedObject's values suddenly going nullNSManagedObject 值突然变为空
【发布时间】:2014-04-13 18:22:47
【问题描述】:

我的申请有问题。在viewDidLoad 方法中,_recipe 对象将打印出它的值。但是,当调用 addToFavourites 方法时,_recipe 对象的值返回 null,我不明白为什么会有任何帮助会受到重视。

这是课程。

  #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

【问题讨论】:

    标签: ios objective-c core-data nsmanagedobject


    【解决方案1】:

    好的,我知道了!对_recipe 的引用是weak!因此,当viewDidLoad 方法结束时,前一个视图被放入堆中。由于_recipe 没有额外的引用计数,它也在堆上!

    【讨论】:

    • 感谢您回来提供您的解决方案。请尽快将您的回复标记为已回答。
    • @flexaddicted 不用担心!将在 23 小时内完成 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 2020-07-05
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    相关资源
    最近更新 更多