From 0932765947b14407aa41fbe7ded00ca37a25f5c6 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 16:58:13 +0200 Subject: [NEW] Protocol Implementation --- objective-c.html.markdown | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 9d1178e1..187ea30a 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -215,20 +215,22 @@ int main (int argc, const char * argv[]) // Declare your class in a header(MyClass.h) file: // Class Declaration Syntax: -// @interface : +// @interface ClassName : ParentClassName // { // Member variable declarations; // } // -/+ (type) Method declarations; // @end -@interface MyClass : NSObject +@interface MyClass : NSObject { int count; id data; NSString *name; } -// Create the public getter/setter for the variable count -@property(assign) int count; +// Convenience notation to auto generate public getter and setter +@property int count; +@property (copy) NSString *name; // Copy the object during assignment. +@property (readonly) id data; // Declare only a getter method. // Methods +/- (return type)methodSignature:(Parameter Type *)parameterName; @@ -246,8 +248,13 @@ int main (int argc, const char * argv[]) @implementation UserObject +// Call when the object is releasing +- (void)dealloc +{ +} + // Constructors are a way of creating classes -// This is a default constructor +// This is a default constructor which is call when the object is creating - (id)init { if ((self = [super init])) @@ -272,8 +279,25 @@ int main (int argc, const char * argv[]) return @42; } +// Methods declared into MyProtocol +- (void)myProtocolMethod +{ + // statements +} + @end +/* + * A protocol declares methods that can be implemented by any class. + * Protocols are not classes themselves. They simply define an interface + * that other objects are responsible for implementing. + * / +@protocol MyProtocol + - (void)myProtocolMethod; +@end + + + ``` ## Further Reading -- cgit v1.2.3