简体   繁体   中英

Object remains null after set

I'm trying to set the properties of an object, but it remains null. Can anyone tell me why, please?

Declaration:

@property (nonatomic, strong) ListItem *listItem

Here's the code:

NSLog(@"Selected: %@", [self.fetchedResultsController objectAtIndexPath:indexPath]);
[listItem setCategory:[self.fetchedResultsController objectAtIndexPath:indexPath]];
NSLog(@"set %@", listItem);

and the output:

2012-06-28 14:47:43.037 MarketList[10508:fb03] Selected: <Category: 0xb72a9e0> (entity: Category; id: 0xb7252e0 <x-coredata://F9BFC1DF-1D80-477E-9BC6-3C0949AD922F/Category/p2> ; data: {
listItem = "<relationship fault: 0x6d29930 'listItem'>";
name = "Teste 2";})
2012-06-28 14:47:43.038 MarketList[10508:fb03] set (null)
  1. there is a semicolon missing in your property declaration (assuming this is just a typo though).

  2. Do you ever actually initialize the ListItem? (ie listItem = [[ListItem alloc] init]; If you don't, the pointer to it will be nil, and would cause exactly this behavior (calls with a nil pointer are just ignored).

Your variable listItem holds nil , not an instance of ListItem. You never actually point it to an instance of ListItem, so it's still nil at the end of your method. Sending a message to nil does not cause an object to magically spring into existence to receive the message — nil just silently swallows it. You have to create your ListItem first and then set its property.

NSLog(@"Selected: %@", [self.fetchedResultsController objectAtIndexPath:indexPath]);
listItem = [self.fetchedResultsController objectAtIndexPath:indexPath];
[listItem setCategory:[self.fetchedResultsController objectAtIndexPath:indexPath]];
NSLog(@"set %@", listItem);

Just alloc the object before using it. Alloc it in viewDidLoad.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM