标签:des style class code http tar
学习笔记-段玉磊
Stanford course
dynamic binding
)Introspection 内省机制 ,也就是说通过指定id是什么类型 响应什么方法 通过if进行判断!
关于内省机制的方法:
[obj performSelector:shootSelector];
[obj performSelector:shootAtSelector withObject:coordinate];
[array makeObjectsPerformSelector:shootSelector];//让数组所有元素执行
[array makeObjectsPerformSelector:shootAtSelector withObject:target];
协议机制:
id <UIScrollViewDelegate> scrollViewDelegate;
使它能够对尖括号中的定义的这一组方法做出回应
@interface Vehicle
- (void)move;
@end
@interface Ship : Vehicle
- (void)shoot;
@end
Ship *s = [[Ship alloc] init];
[s shoot];
[s move];
Vehicle *v = s;
[v shoot] #Would not crash at runtime. But have a Complier warning!
id
-(id)copy;
语义:如果可能,返回该对象的一个不可变副本,如果NSDictionary,NSArray 利用copy是正确的,如果传递一个可变的数组、字典,那么返回的就是一个不可变的类。
-(id)mutableCopy;
语义:不管接收可变或者不可变,都返回可变的。
不要利用下面的方法进行for in
遍历:
NSArray *myArray = ...;
for (NSString *string in myArray){//数组元素可能不包含NSString类型
double value = [string doubleValue];
// Crash here if string is not an NSString
}
通过Introspection方式进行防御式编程:
NSArray *myArray = ...;
for (id obj in myArray){
if([obj isKindOfClass:[NSString class]]){
// send NSString messages to obj with no worries.
}
}
创建NSNumber old方法:
NSNumber *n = [NSNumber numberWithInt:24];
float f = [n floatValue];
新的语法创造NSNumber in iOS 6 : @()
NSNumber *three = @3;
NSNumber *underline = @(NSUnderlineStyleSingle);
NSNumber *match = @([card match:@[otherCard]]);
枚举遍历的方式:
NSDictionary *myDictionary = ...;
for (id key in myDictionary){
// do something with key here
id value = [myDictionary objectForKey:key];
// do something with value here
}
Foundation 学习笔记,布布扣,bubuko.com
标签:des style class code http tar
原文地址:http://www.cnblogs.com/firstrate/p/3804326.html