How can I call a method in Objective-C?

前端 未结 8 1582
隐瞒了意图╮
隐瞒了意图╮ 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:57

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

    0 讨论(0)
  • 2020-12-01 11:02

    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];
    }
    
    0 讨论(0)
提交回复
热议问题