Objective-C Specific Learning

header files    .h    public declarations
source files    .m    implementation details of your code.
#import          same file is only included once

Strong typing and Weak typing
Strong and weak typing for storing objects in variables.
MyClass*  myObject1;    // 强类型(以class为类型,有*)

id myObject2; // 弱类型(以id为类型,常用于集合类)

most frameworks pass strings  in NSString objects

NSString*  myString = @"My Stringn";   //定义对象
NSString*  anotherString = [NSString stringWithFormat:@"%d %s", 1, @"String"];  //设置字符串格式(调用多参方法)

// 用C字符串创建Objective-C字符串
NSString*  fromCString = [NSString stringWithCString:"A C string" encoding:NSASCIIStringEncoding];  //连续调用方法

A class has two pieces: the interface and the implementation.
The interface contains the class declaration and defines the instance variables and methods .
The implementation contains the actual code for the methods of the class.

@interface MyClass: NSObject  //子类继承父类
    int              count;                //成员变量(括起)
    id               data;
    NSString*    name   

-(id)initWithString: (NSString*)aName;                               //实例方法(-)
+(MyClass*)createMyClassWithString: (NSString*)aName;   //类方法(+)

@implementation MyClass
- (id)initWithString:(NSString *) aName   //实现实例方法
    if (self = [super init]) {
        count count = 0;
        data = nil;
        name = [aName copy];
        return self;
+ (MyClass *)createMyClassWithString: (NSString *) aName   //实现类方法
    return [[[self alloc] initWithString:aName] autorelease];
a. Method:
Two types of methods: instance methods(-) and class methods(+).
-(void)insertObject: (id)anObject atIndex:(NSUInteger)index  //方法类型标识符-+(返回值)方法名:(参数类型)参数名

Call a method by "messaging” the corresponding object.
The message  is the method name along with the parameter information.
[myArray insertObject:anObj atIndex:0];  //myArray.insertObject(anObj.atIndex(0)), ":"相当于"()"," "相当于"."

Nest Messages([…[…]…]):
To avoid declaring numerous local variables to store temporary results.
[[myAppObject getArray] insertObject:[myAppObject getObjectToInsert] atIndex:0]  //[]调用方法,返回对象

Class Method:
NSMutableArray*   myArray = nil;    //init myArray
myArray = [NSMutableArray arrayWithCapacity:0];  // Create a new array and assign it to the myArray variable.

b. Properties:
Properties eliminate the need to provide a distinct getter and setter method for each instance.
Instead, you specify the behavior using the property declaration and then synthesize.

Property declarations:
@property BOOL flag;
@property (copy) NSString* nameObject;  // Copy the object during assignment.
@property (readonly) UIView* rootView;  // Create only a getter method.

You can use dot syntax:
myObject.flag = YES;
CGRect   viewFrame = myObject.rootView.frame;

Using methods to implement property:
[myObject setFlag:YES];  //为属性赋值
CGRect   viewFrame = [[myObject rootView] frame];  //多层调用属性=myObject.rootView.frame,[]相当于(),表示优先执行


class的实现: interface(.h–declare class) & implementation(.m–implement class)。

Class的结构: property({list}, omit get/set) & method(+/-, name+para=message)。

           property: @property type instance;     @synthesize  instance;      

           method: +/- (RtType*)methodName: (paraType*) parameter{…}     [objectInstance methodName:parameter]; 

Object type: Strong(class name), Weak(id)。

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s