2011年8月28日 星期日

Objective-C Fundamental

Class Declaration

A class declaration

Objective-C supports both strong and weak typing for variables containing objects

MyClass *myObject1;  // Strong typing
id       myObject2;  // Weak typing


All Objective-C object variables are pointers type. Remember to put the * in front of the variable names for strongly-typed object declarations. The id type implies a pointer.


Method Declaration


Method declaration syntax

  • The (-) sign indicates that this is an instance method.
  •        (+) sign indicates that this is an class method. Equivalent to static method in Java
  • The method’s actual name (insertObject:atIndex:)
  • The colon characters declare the presence of a parameter. If a method has no parameters, you omit the colon after the first (and only) signature keyword. In this example, the method takes two parameters.


Invoke a Method = Send Message to an Object

[object methodName:arg1 withOther:arg2];

For example, to send the insertObject:atIndex: message to an object in the myArray variable, you would use the following syntax:




[myArray insertObject:anObject atIndex:0];

In case of nested message:



[myArray insertObject:[myAppObject objectToInsert] atIndex:0];

or even myArray is nested:



[[myAppObject someArray] insertObject:[myAppObject objectToInsert] atIndex:0];

which is equivalent to:



[myAppObject.someArray insertObject:[myAppObject objectToInsert] atIndex:0];



Access instance variable (object properties)
[photo setCaption:@"Day at the beach"];
output = [photo caption] 
is equivalent to 
photo.caption = @"Day at the beach";
output = photo.caption;


Creating Objects
NSString* myString = [NSString string];
or,
NSString* myString = [[NSString alloc] init]; 


Memory Management (reference counting)
// string1 will be released automatically NSString* string1 = [NSString string]; // must release this when done NSString* string2 = [[NSString alloc] init]; [string2 release];





Properties
Declaration in header file:
      @property (retain) NSString* caption;
- The "retain" specifies that the setter should retain the input value

in implementation file:
      @synthesize caption;
- the @synthesize directive auto generates the setters and getters

and release the memory by : 
       - (void) dealloc{ 
             [caption release]; 
          } 
or,
       - (void) dealloc{
             self.caption = nil; 
          } 




Categories
Categories are one of the most useful features of Objective-C. Essentially, a category allows you to add methods to an existing class without subclassing it or needing to know any of the details of how it's implemented. 

For example, if I wanted to add a method to NSString to determine if the contents is a URL, it would look like this:  
in header file "NSString-Utilities.h"
@interface NSString (Utilities
- (BOOL) isURL; 
@end
in implementation file:
@implementation NSString (Utilities) - (BOOL) isURL { if ( [self hasPrefix:@"http://"] ) return YES; else return NO; } @end
To use, same as normal method invocation:
if( [someString isURL]) NSLog(@"String is a URL");




Reference Website:
http://cocoadevcentral.com/d/learn_objectivec/




沒有留言:

張貼留言