mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-07-31 20:10:49 +02:00
Add example of calling instance and class methods.
This commit is contained in:
@@ -306,10 +306,11 @@ int main (int argc, const char * argv[])
|
|||||||
// Methods
|
// Methods
|
||||||
+/- (return type)methodSignature:(Parameter Type *)parameterName;
|
+/- (return type)methodSignature:(Parameter Type *)parameterName;
|
||||||
|
|
||||||
// + for class method
|
// + for class method.
|
||||||
+ (NSString *)classMethod;
|
+ (NSString *)classMethod;
|
||||||
|
+ (MyClass *)myClassFromName:(NSString *)name;
|
||||||
|
|
||||||
// - for instance method
|
// - for instance methods.
|
||||||
- (NSString *)instanceMethodWithParameter:(NSString *)string;
|
- (NSString *)instanceMethodWithParameter:(NSString *)string;
|
||||||
- (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number;
|
- (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number;
|
||||||
|
|
||||||
@@ -329,6 +330,14 @@ myClass.count = 45;
|
|||||||
NSLog(@"%i", myClass.count); // prints => 45
|
NSLog(@"%i", myClass.count); // prints => 45
|
||||||
|
|
||||||
|
|
||||||
|
// Call class methods:
|
||||||
|
NSString *classMethodString = [MyClass classMethod];
|
||||||
|
MyClass *classFromName = [MyClass myClassFromName:@"Hello"];
|
||||||
|
|
||||||
|
// Call instance methods:
|
||||||
|
MyClass *myClass = [[MyClass alloc] init]; // Create MyClass object instance.
|
||||||
|
NSString *stringFromInstanceMethod = [myClass instanceMethodWithParameter:@"Hello"];
|
||||||
|
|
||||||
// Implement the methods in an implementation (MyClass.m) file:
|
// Implement the methods in an implementation (MyClass.m) file:
|
||||||
@implementation MyClass {
|
@implementation MyClass {
|
||||||
long distance; // Private access instance variable.
|
long distance; // Private access instance variable.
|
||||||
|
Reference in New Issue
Block a user