Passing self as an argument to other objects


I think I understand how to use ‘self’ within an implementation, but I’m having difficulty understanding why or how you’d use it “to let other objects know where the current object is”.

Taking the case in the Chapter of - (void)addYourselfToArray(NSMutableArray *)theArray, please could someone give me an example of when you would use it, and how would you call it?

Many thanks



Right, I’ve figured it out, I think. In case anyone else is having the same confusion (or in case I’ve got it wrong…:wink:), I’ll post what I’ve found.

Given the method outlined in my OP, I can add the object Fred to the NSMultipleArray test with the command

[Fred addYourselfToArray:test];

The reason I was finding it difficult to work out was that when I tried to test this with NSLog(@"%@", [test objectAtIndex:0] I got the result <Person: [hexaddress[>.

I assumed this was because I’d got the addYourselfToArray method wrong but in fact it was because NSLog just doesn’t allow you to print out the internal name of the object in this way – the object had correctly been loaded into the array and the instance variables were accessible, I was just checking for the wrong thing…

An obvious solution in the end, but I’m glad I didn’t see it at first, as I think I’ve learnt quite a bit by having to figure it out.