How can I call a method in Objective-C?

前端 未结 8 1581
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 10:27

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

相关标签:
8条回答
  • 2020-12-01 10:37

    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]
    
    0 讨论(0)
  • 2020-12-01 10:38

    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.

    0 讨论(0)
  • 2020-12-01 10:39

    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

    0 讨论(0)
  • 2020-12-01 10:41
    use this,
    [self score]; 
    instead of @selector(score).
    
    0 讨论(0)
  • 2020-12-01 10:45

    Use this:

    [self score]; you don't need @sel for calling directly
    
    0 讨论(0)
  • 2020-12-01 10:51
    [self score]; instead of @selector(score)
    
    0 讨论(0)
提交回复
热议问题