Sidwyn Koh
Sidwyn Koh

Reputation: 1742

Calling self in objective c method headers

Lets say I made a method that can tabulate the total of x and y.

total = [self totalThemUp x:30 y:50];

Is self correctly used? Why so? I don't see any object in particular that is acted on.

Thanks for your help!

Upvotes: 2

Views: 367

Answers (2)

theMikeSwan
theMikeSwan

Reputation: 4729

provided you have a method called totalThemUpx: y: then self is used correctly. It may not be the best way to handle this situation as noted in the previous answers, but it is the correct way to refer to self. It is worth noting however that in your line of code you have a space between "totalThemUp" and "x:" which will not actually work. A more appropriate method name would be total: with:, or perhaps add: to: as they read a bit better.

Upvotes: 0

dreamlax
dreamlax

Reputation: 95335

If a method doesn't rely on an instance's state, it may be better off as a class method or a standalone function.

Upvotes: 1

Related Questions