views:

49

answers:

2

I'm trying to to return a NSMutableArray but I got this error in the console:

2010-10-01 14:12:21.348 Phonebook[1424:a0f] +[LinkedList getListArray]: unrecognized selector sent to class 0x1000053e8

The method code is:

- (id)getListArray {
 ListNode *tmp = iterator;
 iterator = head;

 NSMutableArray * list = [NSMutableArray arrayWithCapacity:self.getNSIntegerNum];

 while ([iterator next] != nil) {
  [list addObject:[iterator data]];
 }

 iterator = tmp;

 return list;

}

and the method call is:

contacts = [LinkedList getListArray];

"contacts" is a already initialized NSMutableArray object. Any ideas?

+3  A: 

It looks like you are calling getListArray as if it were a class method, but it's defined as an instance method.

You need to either define getListArray as a class method (with a + instead of a - at the beginning), or create an instance of LinkedList and call getListArray on that.

// Best guess code sample.
LinkedList *myLinkedList = [[LinkedList alloc] init];
contacts = [myLinkedList getListArray];
[myLinkedList release];
Robot K
I'm sorry, i didn't paste de code where the class is instantiated. In my code i created a instance just like you did. i have already have working methods in this class. =S wierd.
Leonardo Suzan
@Leonardo Suzan: It doesn't matter if you've created an instance — you are still sending `getListArray` to the class itself. It's right in your question: `[LinkedList getListArray]` — that's a message to the class, not to any particular instance.
Chuck
Can you post the code where you instantiate your LinkedList object? I think it would help tremendously.
Robot K
A: 
contacts = [LinkedList getListArray];

contacts is a already initialized NSMutableArray object.

Or it was. If the message had gone through (that is, if the LinkedList class responded to this message or if you were to send it to a LinkedList instance instead), this assignment would replace the array you previously created and stored into contacts with the array created and returned by getListArray.

Remember: The contacts variable is not an array. It is a container for a pointer to an (array) object. You say you previously created an array, and stored its pointer in this variable; now you are trying to obtain another array and store its pointer in this variable, which will replace whatever pointer was there before.

As it is, the only reason you aren't replacing the contacts array is the reason that Robot K pointed out: You're sending the message to the LinkedList class, but only instances of LinkedList respond to that message.

There are two solutions to both the creation of a redundant array and the exception:

  1. Rename getListArray to getListArray:, and make it take an NSMutableArray as its parameter and fill in that array.
  2. Stop creating an empty array beforehand (you don't need to) and send the getListArray message to an instance of LinkedList rather than to the LinkedList class. The latter part is what Robot K suggested.

I recommend solution #2.

When people make the redundant-creation-of-an-object mistake, they often also leak the redundant object (one example); you haven't shown the creation of the previous array, so I can't see whether you've done that, so, just in case, I'll also suggest that you read or re-read the Memory Management Programming Guide for Cocoa.

Peter Hosey