How to call a method
-
27-04-2021 - |
Question
I have a method with the signature - (void)addStringsToArray
. I want to call it in the viewDidLoad
method. How do I call it?
Solution
Method calls (or, actually, message sends) in Objective-C have the syntax [receiver selector]
.
- (void)viewDidLoad {
// Where MyClass is the class your method is in.
MyClass *object = [[MyClass alloc] init];
[object addStringsToArray];
}
Here, object
is the receiver, and addStringsToArray
is the selector. Use self
as the receiver if your method is in the same class as the current method (i.e. your view controller).
I highly recommend you to read The Objective-C Programming Language. The answer to your question is under "Object Messaging" in the first chapter.
OTHER TIPS
I'm presuming that your confusion is because the method you want to call is inside the same UIViewController class. In that specific case, you're looking for 'self'.
[self addStringsToArray];
If you have wrote this method in same implementation file of your ViewController earlier:
[self addStringsToArray];