(BNRItem *)createItem method question


#1

Implementation for it is:

[code]- (BNRItem *)createItem
{
BNRItem *p = [BNRItem randomItem];

[allItems addObject:p];

return p;

[/code]

And in ItemsViewController there is this method:

- (id)init { // Call the superclass's designated initializer self = [super initWithStyle:UITableViewStyleGrouped]; if (self) { for (int i = 0; i < 5; i++) { [[BNRItemStore sharedStore] createItem]; } } return self; }

And my question is, createItem returns BNRItem, but it returns it where?
This method [[BNRItemStore sharedStore] createItem]; simply calls that method, it doesn’t store value in any variable…

Could you explain please?


#2

Although the method invocation [[BNRItemStore sharedStore] createItem] inside the for loop is not using the value (object pointer) returned by the createItem method, if you look inside that method you will see that the random item object that is created is not only being returned but also being added to the allItems collection. So as a result of the method invocation inside the for loop, five objects are being created and added to the allItems collection, but the objects that are returned are simply being discarded. This is what may have caused you to feel disoriented.


#3

Yes, I saw that it is adding items to allItems, but what is the point then to return BNRItem if it’s being discarded?


#4

Yes, the BNRItem returned in this chapter of the book is being discarded, however it is actually used in a later chapter.

Without referring back to the text, I assume however that this is not explained within chapter 9.

Nick
http://myfirstiphoneapp.co.uk
http://easyintervalsapp.co.uk


#5

That would explain it :slight_smile:
Thx :wink:


#6

Thanks for the answer! I was also stumped on this and wondering why the heck i’m returning something that isn’t being used.

Thanks!!