57

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?

Georg Schölly
  • 116,521
  • 48
  • 204
  • 258

8 Answers8

96

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

Igor Barinov
  • 12,534
  • 10
  • 26
  • 33
nduplessis
  • 11,882
  • 2
  • 34
  • 53
31

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.

NXT
  • 1,861
  • 22
  • 29
Chuck
  • 222,660
  • 29
  • 289
  • 383
  • 7
    @fijiaaron: I didn't say "RTFM." I said that this question was on such an elementary level that if he had to ask it, he was going to find his experience in Objective-C very frustrating and reading that introductory guide was a good start down the right path. Teach a man to fish, you know. Personally, I find this kind of answer much more helpful than a code-vomit of unknown quality. – Chuck Oct 12 '11 at 15:40
24

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 well enough to give you more information on that.

chicks
  • 1,901
  • 2
  • 19
  • 32
Ferruccio
  • 93,779
  • 37
  • 217
  • 294
  • @selector gives you the "name" of a method in a form that Objective-C can use to send messages. It's used somewhat analogously to a function pointer. – Chuck Feb 26 '09 at 19:23
22

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]
Mohammed Rashwan
  • 229
  • 2
  • 10
  • This seems clear and includes a Parameter, which is helpful, even if not for the simple "score" example. – Matt Dec 17 '14 at 17:06
5

Use this:

[self score]; you don't need @sel for calling directly
ganesh manoj
  • 953
  • 10
  • 24
  • 2
    I been wondering for a while if adding delay to a method was possible. Never had the requirement to do it, but always wonder if could be done and on how it would be done. :) nice one. Came for one thing and ended up finding something useful. – Jiraheta Aug 07 '14 at 18:04
2

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];
}
Sohaib Aslam
  • 806
  • 12
  • 21
1
[self score]; instead of @selector(score)
codercat
  • 21,439
  • 9
  • 56
  • 84
PICKme
  • 31
  • 4
1
use this,
[self score]; 
instead of @selector(score).
manishkumar
  • 543
  • 2
  • 5
  • 21