summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorLevi Bostian <levi.bostian@gmail.com>2014-01-08 23:37:43 -0600
committerLevi Bostian <levi.bostian@gmail.com>2014-01-29 21:44:05 -0600
commit4b44b03a07bf6ffc1f265fa0f08e8ef8ceeae8d3 (patch)
tree7e47fbdb1cd46c8c96ca74b2b90d5bdcddd69364
parente3853e564bea5a1463ee7b1ec4b9beaaae295d0c (diff)
Add extensions description with example.
-rw-r--r--objective-c.html.markdown36
1 files changed, 36 insertions, 0 deletions
diff --git a/objective-c.html.markdown b/objective-c.html.markdown
index 33200b63..47af5ae8 100644
--- a/objective-c.html.markdown
+++ b/objective-c.html.markdown
@@ -553,6 +553,42 @@ int main (int argc, const char * argv[]) {
@end
+// Extensions
+// Extensions allow you to override public access property attributes and methods of an @interface.
+// @interface filename: Shape.h
+@interface Shape : NSObject // Base Shape class extension overrides below.
+
+@property (readonly) NSNumber *numOfSides;
+
+- (int)getNumOfSides;
+
+@end
+// You can override numOfSides variable or getNumOfSides method to edit them with an extension:
+// @implementation filename: Shape.m
+#import "Shape.h"
+// Extensions live in the same file as the class @implementation.
+@interface Shape () // () after base class name declares an extension.
+
+@property (copy) NSNumber *numOfSides; // Make numOfSides copy instead of readonly.
+-(NSNumber)getNumOfSides; // Make getNumOfSides return a NSNumber instead of an int.
+-(void)privateMethod; // You can also create new private methods inside of extensions.
+
+@end
+// The main @implementation:
+@implementation Shape
+
+@synthesize numOfSides = _numOfSides;
+
+-(NSNumber)getNumOfSides { // All statements inside of extension must be in the @implementation.
+ return _numOfSides;
+}
+-(void)privateMethod {
+ NSLog(@"Private method created by extension. Shape instances cannot call me.");
+}
+
+@end
+
+
// Protocols
// A protocol declares methods that can be implemented by any class.
// Protocols are not classes themselves. They simply define an interface