From 5ad738af38b4642257f5036ac5ed7dbc27f4e187 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Wed, 1 Jan 2014 17:20:23 -0600 Subject: [PATCH 1/4] Add example of calling instance and class methods. --- objective-c.html.markdown | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/objective-c.html.markdown b/objective-c.html.markdown index a70351b5..e1834bf8 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -306,10 +306,11 @@ int main (int argc, const char * argv[]) // Methods +/- (return type)methodSignature:(Parameter Type *)parameterName; -// + for class method +// + for class method. + (NSString *)classMethod; ++ (MyClass *)myClassFromName:(NSString *)name; -// - for instance method +// - for instance methods. - (NSString *)instanceMethodWithParameter:(NSString *)string; - (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number; @@ -329,6 +330,14 @@ myClass.count = 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: @implementation MyClass { long distance; // Private access instance variable. From 7496526cf447c6f5459a9b32448d2ba6e0f60dc0 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Mon, 6 Jan 2014 20:18:45 -0600 Subject: [PATCH 2/4] Add more examples of methods available to objects. --- objective-c.html.markdown | 51 ++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/objective-c.html.markdown b/objective-c.html.markdown index e1834bf8..cdf89338 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -297,9 +297,10 @@ int main (int argc, const char * argv[]) } // Convenient notation for public access variables to auto generate a setter method. // By default, setter method name is 'set' followed by @property variable name. -@property int count; // Setter method name = 'setCount' -@property (copy) NSString *name; // (copy) => Copy the object during assignment. -@property (readonly) id data; // (readonly) => Cannot set value outside interface. +@property int propInt; // Setter method name = 'setCount' +@property (copy) id copyId; // (copy) => Copy the object during assignment. +// (readonly) => Cannot set value outside interface. +@property (readonly) NSString *roString; // Use @synthesize in @implementation to create accessor. // You can customize the getter and setter names instead of using default 'set' name: @property (getter=lengthGet, setter=lengthSet:) int length; @@ -308,12 +309,15 @@ int main (int argc, const char * argv[]) // + for class method. + (NSString *)classMethod; -+ (MyClass *)myClassFromName:(NSString *)name; ++ (MyClass *)myClassFromHeight:(NSNumber *)defaultHeight; // - for instance methods. - (NSString *)instanceMethodWithParameter:(NSString *)string; - (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number; +// Constructor methods with arguments: +- (id)initWithDistance:(int)defaultDistance; + @end // States the end of the interface. @@ -341,6 +345,7 @@ NSString *stringFromInstanceMethod = [myClass instanceMethodWithParameter:@"Hell // Implement the methods in an implementation (MyClass.m) file: @implementation MyClass { long distance; // Private access instance variable. + NSNumber height; } // To access a public variable from the interface file, use '_' followed by variable name: @@ -348,27 +353,49 @@ _count = 5; // References "int count" from MyClass interface. // Access variables defined in implementation file: distance = 18; // References "long distance" from MyClass implementation. -// Call when the object is releasing -- (void)dealloc +// Called before calling any class methods or instantiating any objects. ++ (void)initialize { + if (self == [MyClass class]) { + distance = 0; + } } -// Constructors are a way of creating classes +// Counterpart to initialize method. Called when an object's reference count is zero. +- (void)dealloc +{ + [height release]; // If not using ARC, make sure to release class variable objects + [super dealloc]; // and call parent class dealloc. +} + +// Constructors are a way of creating instances of classes. // This is a default constructor which is called when the object is initialized. - (id)init { - if ((self = [super init])) + if ((self = [super init])) // 'super' used to access methods from parent class. { - self.count = 1; + self.count = 1; // 'self' used for object to send messages to itself. } return self; } +// Can create constructors that contain arguments: +- (id)initWithDistance:(int)defaultDistance +{ + distance = defaultDistance; + return self; +} + (NSString *)classMethod { return [[self alloc] init]; } ++ (MyClass *)myClassFromHeight:(NSNumber *)defaultHeight +{ + height = defaultHeight; + return [[self alloc] init]; +} + - (NSString *)instanceMethodWithParameter:(NSString *)string { return @"New string"; @@ -379,6 +406,12 @@ distance = 18; // References "long distance" from MyClass implementation. return @42; } +// If you create a method in @implementation but do not include in @interface, it is private. +- (NSNumber *)secretPrivateMethod { + return @72; +} +[self secretPrivateMethod]; // Calls private method. + // Methods declared into MyProtocol - (void)myProtocolMethod { From a16841e0497d033af29257233b2c3e8e93b7987f Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Mon, 6 Jan 2014 22:18:54 -0600 Subject: [PATCH 3/4] Add selector and @synthesize examples. --- objective-c.html.markdown | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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 From eab35e826eb86744d98a3fc424ebca6ccf432390 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Wed, 8 Jan 2014 13:03:31 -0600 Subject: [PATCH 4/4] Organize branch changes. --- objective-c.html.markdown | 46 ++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 38b2cc52..f2787649 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -297,9 +297,9 @@ int main (int argc, const char * argv[]) } // Convenient notation for public access variables to auto generate a setter method. // By default, setter method name is 'set' followed by @property variable name. -@property int propInt; // Setter method name = 'setCount' +@property int propInt; // Setter method name = 'setPropInt' @property (copy) id copyId; // (copy) => Copy the object during assignment. -// (readonly) => Cannot set value outside interface. +// (readonly) => Cannot set value outside @interface. @property (readonly) NSString *roString; // Use @synthesize in @implementation to create accessor. // You can customize the getter and setter names instead of using default 'set' name: @property (getter=lengthGet, setter=lengthSet:) int length; @@ -307,16 +307,17 @@ int main (int argc, const char * argv[]) // Methods +/- (return type)methodSignature:(Parameter Type *)parameterName; -// + for class method. +// + for class methods: + (NSString *)classMethod; + (MyClass *)myClassFromHeight:(NSNumber *)defaultHeight; -// - for instance methods. +// - for instance methods: - (NSString *)instanceMethodWithParameter:(NSString *)string; - (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number; // Constructor methods with arguments: - (id)initWithDistance:(int)defaultDistance; +// Objective-C method names are very descriptive. Always name methods according to their arguments. @end // States the end of the interface. @@ -333,21 +334,6 @@ 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]; MyClass *classFromName = [MyClass myClassFromName:@"Hello"]; @@ -356,6 +342,22 @@ MyClass *classFromName = [MyClass myClassFromName:@"Hello"]; MyClass *myClass = [[MyClass alloc] init]; // Create MyClass object instance. NSString *stringFromInstanceMethod = [myClass instanceMethodWithParameter:@"Hello"]; +// Selectors. +// Way to dynamically represent methods. Used to call methods of a class, pass methods +// through functions to tell other classes they should call it, and to save methods +// as a variable. +// SEL is the 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 all method arguments into one object to send to performSelector function. + NSArray *arguments = [NSArray arrayWithObjects:@"Hello", @4, nil]; + [myClass performSelector:selectorVar withObject:arguments]; // Calls the method. +} else { + // NSStringFromSelector() returns a NSString of the method name of a given selector. + NSLog(@"MyClass does not have method: %@", NSStringFromSelector(selectedVar)); +} + // Implement the methods in an implementation (MyClass.m) file: @implementation MyClass { long distance; // Private access instance variable. @@ -384,13 +386,13 @@ distance = 18; // References "long distance" from MyClass implementation. [super dealloc]; // and call parent class dealloc. } -// Constructors are a way of creating instances of classes. +// Constructors are a way of creating instances of a class. // This is a default constructor which is called when the object is initialized. - (id)init { if ((self = [super init])) // 'super' used to access methods from parent class. { - self.count = 1; // 'self' used for object to send messages to itself. + self.count = 1; // 'self' used for object to call itself. } return self; } @@ -422,7 +424,7 @@ distance = 18; // References "long distance" from MyClass implementation. return @42; } -// If you create a method in @implementation but do not include in @interface, it is private. +// To create a private method, create the method in the @implementation but not in the @interface. - (NSNumber *)secretPrivateMethod { return @72; }