From 4b44b03a07bf6ffc1f265fa0f08e8ef8ceeae8d3 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Wed, 8 Jan 2014 23:37:43 -0600 Subject: [PATCH] Add extensions description with example. --- objective-c.html.markdown | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) 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