Objective-C New Features

Strong and Weak

1. 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.
                 Or if I myself am removed from the heap because no one strongly points to me!

2. 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).


Initializer

1. Designated Initializer方法是每个类保证继承的实例变量被初始化的方法,每个类的指定初始化方法都必须调用父类的指定初始化方法

2. 该类中其他初始化方法必须调用该类的指定初始化方法



Property List

1. The term “Property List” just means a collection of collections Specifically, it is any graph of objects containing only the following classes:
NSArray, NSDictionary, NSNumber, NSString, NSDate, NSData


Strong and Weak

1. 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. Or if I myself am removed from the heap because no one strongly points to me!

2. Weak --- 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).

nil Nil Null

1. nil表示一个Objctive-C对象指针,这个指针指向空(nil值为0)。
首字母大写的Nil和nil有一点不一样,Nil定义一个指向空的类(是Class,而不是对象)。

在Objective-C里,nil对象被设计来跟NULL空指针关联的。他们的区别就是nil是一个对象,而NULL只是一个值。而且我们对于nil调用方法,不会产生crash或者抛出异常。



动态特性

1. 动态输入

    在运行时才决定一个对象的类,通过使用id数据类型。与动态输入响应的是静态输入,编译时就指定对象的类。动态类型id表示一个指针,而声明静态类型的。运行时通过isa变量获取对象的相关信息。

    需要在对象名前加*表示指针。对象的引用必须为指针。

2. 动态绑定

    在运行时才决定需要调用的方法,消息被发送时才和响应的方法绑定

3. 动态载入

    在运行时添加代码块和其他资源。

4. 多态

    不同的对象可以按不同的方式响应同名的消息


类和对象


1. 对象是类在运行时的实例,包含类声明的实例变量和指向方法的指针,拥有自己的内存空间。

2. 类对象在在运行时创建的class类型对象,

3. nil定义为一个null对象,一个值为0的id类型数据

4. The compiler examines the strength of references in an object graph and adds retain and release messages where appropriate.

5. A strong reference indicates ownership; the referring object owns the referenced object. A weak reference implies that the referring object does not own the referenced object. The lifetime of an object is determined by how many strong references there are to it. An object is not freed as long as there is a strong reference to it.

6. References in Objective-C are strong by default.


7. Consequently, a strong reference cycle can cause your program to leak memory. 

__block typeof(self) tmpSelf = self;

[self methodThatTakesABlock:^ {

    [tmpSelf doSomething];

}];

8. A block forms a strong reference to variables it captures. If you use self within a block, the block forms a strong reference to self, so if self also has a strong reference to the block (which it typically does), a strong reference cycle results. To avoid the cycle you need to create a weak (or __block) reference to self outside the block, as in the example above.


9.类定义了对象的原型描述,实例变量和方法
10.编译器为每个类仅仅创建一个类对象,类对象知道如何创建实例对象
11.每个实例对象拥有自己私有的变量,但是分享相同的方法
12.类对象没有实例变量,但是有方法(包含父类的方法)


类对象

1.

2.类对象没有任何实例变量,也不能响应实例方法,但是类对象可以响应类方法
3.在源代码中,类名只在作为消息接收者的时候用来表示类对象,其他时候需要使用下列方法来获取类对象
id aClass = [anObject class];
id rectClass = [Rectangle class];
4.所有的类对象都属于Class类型,你也可以指定一个类对象为id类型


方法和消息

1. allocation设置所有的实例变量为0

2. NSDate

NSDate *now = [NSDate date]; // 1

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; // 2

[calendar setTimeZone:[NSTimeZone systemTimeZone]]; // 3

NSDateComponents *dc = [calendar components:(NSHourCalendarUnit|NSMinuteCalendarUnit|

    NSSecondCalendarUnit) fromDate:now];  // 4

NSLog(@"The time is %d:%d:%d", [dc hour], [dc minute], [dc second]); // 5

3. Enumerate using block
NSArray *myArray = // get array

[myArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

    if ([(NSString *)obj isEqualToString:@"Cupertino"]) {

        NSLog(@"We're near the mothership!");

        *stop = YES;

    }

}];


4. Introspection自省

static int sum = 0;

for (id item in myArray) {

    if ([item isKindOfClass:[NSNumber class]]) {

        int i = (int)[item intValue];

        sum += i;

    }

}

if ([item respondsToSelector:@selector(setState:)]) {

    [item setState:[self.arcView.font isBold] ? NSOnState : NSOffState];

}

- (void) setDelegate:(id __weak) obj {

    NSParameterAssert([obj conformsToProtocol:

        @protocol(SubviewTableViewControllerDataSourceProtocol)]);

    delegate = obj;

}

5. 对象的比较,如果使用==,检查两个对象是否指向同一个对象,而isEqual检查两个对象的内容是否相同

BOOL objectsAreEqual = [obj1 isEqual:obj2];

if (objectsAreEqual) {

    // do something...

}

6. 复制对象copy。被复制的对象必须要遵守NSCopying协议。深复制,复制对象的所有实例变量和属性。浅复制,复制对象实例变量和属性的引用

7. 因为方法名用于选择一个方法,因此方法名被称为selectors,如果方法包含参数,那么方法名要加:号



Block



int result = myBlock(4); // result is 28

1. block是封装了一段代码的对象,可以作为方法的参数传递,或者作为方法的返回值。block可以定义自己的参数类型和返回值,block用^标记

2. block可以访问局部变量,实例变量,方法的参数,但这个访问是只读的。如果用__block声明变量,则该变量的值在block内可改变。

[UIView animateWithDuration:0.2 animations:^{

    view.alpha = 0.0;

    } completion: ^(BOOL finished) {

    if (finished == YES)

        [view removeFromSuperview];

}];


协议和分类


1. 协议定义了一组由其他对象来实现的方法, 定义了一个由其他类负责实现的接口

2. 分类用来扩展类的接口,添加的方法会被所有的子类继承,也可以被所有的实例对象调用

3. 分类中添加的方法会替代类中同名的方法

4. 分类可以给类添加协议

5. Extension是Category的一个特例,其名字为匿名(为空),并且新添加的方法一定要予以实现。(Category没有这个限制)

6. 正式协议中的方法必须要实现(除了@optional),而非正式协议则不要求。非正式协议一般通过分类来实现

7. 如果category和protocol中定义了property,在实现他们的类的API中应该再次声明。

@protocol Collection
- (BOOL)isEmpty;
@end

@interface NSArray (Collection) <Collection> 
@end 

@implementation NSArray (Collection)
- (BOOL)isEmpty {
return [self count] == 0; }
@end

@interface MyClass () { //注意此处:扩展  
    float value;  
}  
- (void)setValue:(float)newValue;  
@end  



Frameworks


1.


self and super

1. self指代消息的接收者,可能是实例对象或者类对象。

2. super只能作为消息的接收者,表示调用父类的方法而不是当前类的方法

3. isa在根对象中定义,并且所有对象都要继承的一个特殊的实例变量,指向Class对象,用于在运行时识别对象所属的类。







































  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值