objective-c

why properties

instance variables

it is not required to have an instance variable backing up a @property(just skip @synthesize).

some @property might be calculated(usually readonly) rather than stored.

Note: Historically, the interface required declarations of a class’s instance variables, the data structures that are part of each instance of the class. These were declared in braces after the @interface declaration and before method declarations:

Instance variables represent an implementation detail, and should typically not be accessed outside of the class itself. Moreover, you can declare them in the implementation block or synthesize them using declared properties. Typically you should not, therefore, declare instance variables in the public interface and so you should omit the braces.

check

doc from apple dev center

 

dot notation

make access to @property stand out from normal method calls.

similar to c structs.

typedef struct {
    float x;
    float y;
} CGPoint;

@property CGPoint center;

strong vs weak

strong "keep this in the heap until I don't point to it anymore"

I won't point to it anymore if I set my pointer to it to nil.

weak "keep this as long as someone else points to it strongly"

if it gets thrown out of the heap, set my pointer to it to nil automatically( if user on iOS 5 only).

it's reference counting done automatically for you.

nil

if send msg to nil, no code gets executed.

int i = [obj method];
//if obj is nil, i will be 0

but if the method returns a c struct, return value is undefined.

instance method vs static method

instance method starts with a dash

static method starts with a plus sign

- (BOOL) dropBomb: (Bomb * ) bomb
                        at: (CGPoint ) position;
//Bomb * vs CGPoint struct
//one in heap, we are passing around pointers to it, the other is passed in stack

calling syntax

//instance method
Shipe * ship = ...;
[ship dropBomb: at: ];

//class method
[Ship methodShip];
or
[[ship class] doSomething];

in the class method,

self means "this class's class methods"

super means "this class's super class methods"

 

Instantiation

allocating and initializing an object from scratch

NSMutableArray * stack = [[NSMutableArray alloc] init];

check NSObject for alloc and init methods

static typing of initializers

for subclassing reasons, init methods should be typed to return id(not static typed)

@implementation MYObject
- (id) init
{
    self = [super init];//super
    if(self){
    //init our subclass here
    }
    return self;
}
@end

dynamic binding

all objects are allocated in the heap, so you always use a pointer 

NSString * s = ...;//static typed
id obj = s;
//never use "id *"

introspection

NSObject knows these

isKindOfClass
isMemberOfClass
respondsToSelector

@selector() directive turns the name of a method into a selector

if ([obj respondsToSelector:@selector(shoot)]){
    [obj shoot];
}

 SEL is the objective-c "type" for a selector; typedef

SEL shootSelector = @selector(shoot);
[obj performSelector:shootSelector withObject:target];

in UIButton

[button addTarget:self action:@selector(digitPressed:)...];

 

 

 

 

 

 

 

posted on 2012-04-21 18:17  grep  阅读(326)  评论(0编辑  收藏  举报