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 particula开发者_StackOverflow中文版r that is acted on.
Thanks for your help!
If a method doesn't rely on an instance's state, it may be better off as a class method or a standalone function.
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.
精彩评论