diff options
author | Yannick Loriot <yannick.loriot@gmail.com> | 2013-08-13 16:46:16 +0200 |
---|---|---|
committer | Yannick Loriot <yannick.loriot@gmail.com> | 2013-08-13 16:46:16 +0200 |
commit | 0d41a6405627b50f2839df3a3b019836f361ecc0 (patch) | |
tree | b2475c0d84358bd677c814ac5136821b7dfeca1d /objective-c.html.markdown | |
parent | 50c3526247810078b6c63e2cbaf79bc226e873dc (diff) |
[UPDATE] Object Creation
Diffstat (limited to 'objective-c.html.markdown')
-rw-r--r-- | objective-c.html.markdown | 46 |
1 files changed, 36 insertions, 10 deletions
diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 6eac69a8..f43081cf 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -201,27 +201,56 @@ int main (int argc, const char * argv[]) /////////////////////////////////////// // Declare your class in a header(.h) file: - -@interface UserObject : NSObject +// Class Declaration Syntax: +// @interface <class name> : <parent classname> +// { +// Member variable declarations; +// } +// -/+ (type) Method declarations; +// @end +@interface MyClass : NSObject { - // instance variables + int count; + id data; + NSString *name; } +// Create the public getter/setter for the variable count +@property(assign) int count; + +// Methods ++/- (return type)methodSignature:(Parameter Type *)parameterName; -// Class method +// + for class method + (NSString *)classMethod; -// Instance method +// - for instance method - (NSString *)instanceMethodWithParmeter:(NSString *)string; - +- (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number; +- @end // Implement the methods in an implementation (.m) file: @implementation UserObject +// Constructors are a way of creating classes +// This is a default constructor +- (id)init +{ + if ((self = [super init])) + { + self.count = 1; + + // Create an object instance by allocating memory and initializing it. + // An object is not fully functional until both steps have been completed. + UserObject *someObject = [[UserObject alloc] init]; + } + return self; +} + + (NSString *)classMethod { - return @"SomeString"; + return [[self alloc] init]; } - (NSString *)instanceMethodWithParmeter:(NSString *)string @@ -236,9 +265,6 @@ int main (int argc, const char * argv[]) @end -// Create an object instance by allocating memory and initializing it. An object is not fully functional until both steps have been completed. -UserObject *someObject = [[UserObject alloc] init]; - ##Calling Methods // The Objective-C model of object-oriented programming is based on message passing to object instances. |