How can I call a method in Objective-C?

Objective C

Objective C Problem Overview


I am trying to build an iPhone app. I created a
method like this:

- (void)score {
    // some code
}

and I have tried to call it in an other method like this:

- (void)score2 {
    @selector(score);
}

But it does not work. So, how do I call a method correctly?

Objective C Solutions


Solution 1 - Objective C

To send an objective-c message in this instance you would do

[self score];

I suggest you read the Objective-C programming guide Objective-C Programming Guide

Solution 2 - Objective C

I suggest you read The Objective-C Programming Language. The part about messaging is specifically what you want here, but the whole thing will help you get started. After that, maybe try doing a few tutorials to get a feel for it before you jump into making your own apps.

Solution 3 - Objective C

I think what you're trying to do is:

-(void) score2 {
    [self score];
}

The [object message] syntax is the normal way to call a method in objective-c. I think the @selector syntax is used when the method to be called needs to be determined at run-time, but I don't know [tag:objective-c] well enough to give you more information on that.

Solution 4 - Objective C

calling the method is like this

[className methodName] 

however if you want to call the method in the same class you can use self

[self methodName] 

all the above is because your method was not taking any parameters

however if your method takes parameters you will need to do it like this

[self methodName:Parameter]

Solution 5 - Objective C

Use this:

[self score]; you don't need @sel for calling directly

Solution 6 - Objective C

syntax is of objective c is

> returnObj = [object functionName: parameters];

Where object is the object which has the method you're calling. If you're calling it from the same object, you'll use 'self'. This tutorial might help you out in learning Obj-C.

In your case it is simply

> [self score];

If you want to pass a parameter then it is like that

- (void)score(int x) {
    // some code
}

and I have tried to call it in an other method like this:

- (void)score2 {
    [self score:x];
}

Solution 7 - Objective C

[self score]; instead of @selector(score)

Solution 8 - Objective C

use this,
[self score]; 
instead of @selector(score).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionashishView Question on Stackoverflow
Solution 1 - Objective CnduplessisView Answer on Stackoverflow
Solution 2 - Objective CChuckView Answer on Stackoverflow
Solution 3 - Objective CFerruccioView Answer on Stackoverflow
Solution 4 - Objective CMohammed RashwanView Answer on Stackoverflow
Solution 5 - Objective Cganesh manojView Answer on Stackoverflow
Solution 6 - Objective CSohaib AslamView Answer on Stackoverflow
Solution 7 - Objective CPICKmeView Answer on Stackoverflow
Solution 8 - Objective CManish KumarView Answer on Stackoverflow