Junior
Junior

Reputation: 3

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)

Upvotes: 0

Views: 189

Answers (4)

itsji10dra
itsji10dra

Reputation: 4675

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

Upvotes: 0

The Lazy Coder
The Lazy Coder

Reputation: 11818

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

Upvotes: 0

Chuck
Chuck

Reputation: 237010

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.

Upvotes: 1

Dima
Dima

Reputation: 23624

  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).

Upvotes: 1

Related Questions