Calling self in object c methods

Suppose I made a method that can add the total of x and y.

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

Is it used correctly? Why is that? I do not see any objects, in particular, which are active.

Thanks for your help!

+2


a source to share


2 answers


If a method doesn't rely on instance state, it might be better as a class method or stand-alone function.



+1


a source


if you have a method called totalThemUpx: y:

then self is being used correctly. It might not be the best way to handle this situation as pointed out in the previous answers, but it is the correct way to reference yourself. However, it's worth noting that in your line of code, you have a space between "totalThemUp" and "x:" which doesn't actually work. A more appropriate method name would be, total: with:

or perhaps add: to:

because they read slightly better.



+1


a source







All Articles