summaryrefslogtreecommitdiffhomepage
path: root/objective-c.html.markdown
diff options
context:
space:
mode:
authorLevi Bostian <levi.bostian@gmail.com>2014-01-06 22:18:54 -0600
committerLevi Bostian <levi.bostian@gmail.com>2014-01-08 12:45:40 -0600
commita16841e0497d033af29257233b2c3e8e93b7987f (patch)
tree934b53b2f54a5b2ac2badc3bcbe99a286c5b355e /objective-c.html.markdown
parent7496526cf447c6f5459a9b32448d2ba6e0f60dc0 (diff)
Add selector and @synthesize examples.
Diffstat (limited to 'objective-c.html.markdown')
-rw-r--r--objective-c.html.markdown16
1 files changed, 16 insertions, 0 deletions
diff --git a/objective-c.html.markdown b/objective-c.html.markdown
index cdf89338..38b2cc52 100644
--- a/objective-c.html.markdown
+++ b/objective-c.html.markdown
@@ -333,6 +333,20 @@ NSLog(@"%i", [myClass lengthGet]); // prints => 32
myClass.count = 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:
NSString *classMethodString = [MyClass classMethod];
@@ -352,6 +366,8 @@ NSString *stringFromInstanceMethod = [myClass instanceMethodWithParameter:@"Hell
_count = 5; // References "int count" from MyClass interface.
// Access variables defined in implementation file:
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.
+ (void)initialize