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:)...];

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/grep/archive/2012/04/21/2462185.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值