mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
Add selector and @synthesize examples.
This commit is contained in:
parent
7496526cf4
commit
a16841e049
@ -333,6 +333,20 @@ NSLog(@"%i", [myClass lengthGet]); // prints => 32
|
|||||||
myClass.count = 45;
|
myClass.count = 45;
|
||||||
NSLog(@"%i", myClass.count); // prints => 45
|
NSLog(@"%i", myClass.count); // prints => 45
|
||||||
|
|
||||||
|
// Selectors.
|
||||||
|
// Way of dynamically represent methods. Used to call methods of a class, pass methods
|
||||||
|
// through functions to tell other classes they should call it, and save to methods
|
||||||
|
// as a variable.
|
||||||
|
// SEL is data type. @selector() returns a selector from method name provided.
|
||||||
|
// methodAParameterAsString:andAParameterAsNumber: is method name for method in MyClass
|
||||||
|
SEL selectorVar = @selector(methodAParameterAsString:andAParameterAsNumber:);
|
||||||
|
if ([myClass respondsToSelector:selectorVar]) { // Checks if class contains method.
|
||||||
|
// Must put method arguments into one object to send to performSelector.
|
||||||
|
NSArray *arguments = [NSArray arrayWithObjects:@"Hello", @4, nil];
|
||||||
|
[myClass performSelector:selectorVar withObject:arguments]; // Calls the method
|
||||||
|
} else {
|
||||||
|
NSLog(@"MyClass does not have method: %@", NSStringFromSelector(selectedVar));
|
||||||
|
}
|
||||||
|
|
||||||
// Call class methods:
|
// Call class methods:
|
||||||
NSString *classMethodString = [MyClass classMethod];
|
NSString *classMethodString = [MyClass classMethod];
|
||||||
@ -352,6 +366,8 @@ NSString *stringFromInstanceMethod = [myClass instanceMethodWithParameter:@"Hell
|
|||||||
_count = 5; // References "int count" from MyClass interface.
|
_count = 5; // References "int count" from MyClass interface.
|
||||||
// Access variables defined in implementation file:
|
// Access variables defined in implementation file:
|
||||||
distance = 18; // References "long distance" from MyClass implementation.
|
distance = 18; // References "long distance" from MyClass implementation.
|
||||||
|
// To use @property variable in implementation, use @synthesize to create accessor variable:
|
||||||
|
@synthesize roString = _roString; // _roString available now in @implementation.
|
||||||
|
|
||||||
// Called before calling any class methods or instantiating any objects.
|
// Called before calling any class methods or instantiating any objects.
|
||||||
+ (void)initialize
|
+ (void)initialize
|
||||||
|
Loading…
Reference in New Issue
Block a user