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:
- Rename
getListArray
to getListArray:
, and make it take an NSMutableArray as its parameter and fill in that array.
- 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.