首页 > 代码库 > Foundation 学习笔记

Foundation 学习笔记

<link rel="stylesheet" href="http://www.mamicode.com/redesign/global/css/noteView.css"> 笔记内容 <body id="note-frame-body">

学习笔记-段玉磊 Stanford course

Foundation and Attributed Strings


Dynamic binding

  • id 是一个指向任何未知对象的指针,(the consept of dynamic binding)
  • 静态类型化:id 不会发生警告 而NSString *s 会发生警告!
  • Nerver use iD ,因为id只是指针,很危险!!
  • 强制转换需要保护!

id保护机制

Introspection 内省机制 ,也就是说通过指定id是什么类型 响应什么方法 通过if进行判断!
关于内省机制的方法:

  • isKindOfClass:是否是这个类或者子类(类包括继承)
  • isMemberOfClass: 是否是这个类(类不包括继承)
  • responsToSelector:是否是类中的某个方法
  • performSelector: 执行方法
[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

Foundatin Framwork

NSObject

-(id)copy;
语义:如果可能,返回该对象的一个不可变副本,如果NSDictionary,NSArray 利用copy是正确的,如果传递一个可变的数组、字典,那么返回的就是一个不可变的类。
-(id)mutableCopy;
语义:不管接收可变或者不可变,都返回可变的。

NSArray

不要利用下面的方法进行for in遍历:

NSArray *myArray = ...;
for (NSString *string in myArray){//数组元素可能不包含NSString类型
    double value = http://www.mamicode.com/[string doubleValue];"color: #7f9f7f;">// 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

创建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

枚举遍历的方式:

NSDictionary *myDictionary = ...;
for (id key in myDictionary){
    // do something with key here
    id value = http://www.mamicode.com/[myDictionary objectForKey:key];"color: #7f9f7f;">// do something with value here
}

@%28%u5B66%u4E60%u7B14%u8BB0-%u6BB5%u7389%u78CA%29%5BStanford%20course%5D%0AFoundation%20and%20Attributed%20Strings%0A%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%0A%0A%0A%0A-%20-%20-%0A%0A%23%23%20Dynamic%20binding%0A%0A-%20id%20%u662F%u4E00%u4E2A%u6307%u5411%u4EFB%u4F55%u672A%u77E5%u5BF9%u8C61%u7684%u6307%u9488%uFF0C%28the%20consept%20of%20%60dynamic%20binding%60%29%0A-%20%u9759%u6001%u7C7B%u578B%u5316%uFF1Aid%20%u4E0D%u4F1A%u53D1%u751F%u8B66%u544A%20%u800CNSString%20*s%20%20%20%u4F1A%u53D1%u751F%u8B66%u544A%uFF01%0A-%20Nerver%20use%20iD%20%2C%u56E0%u4E3Aid%u53EA%u662F%u6307%u9488%uFF0C%u5F88%u5371%u9669%uFF01%uFF01%0A-%20%u5F3A%u5236%u8F6C%u6362%u9700%u8981%u4FDD%u62A4%uFF01%0A%0A%23%23%23%23%20id%u4FDD%u62A4%u673A%u5236%20%20%0A%0AIntrospection%20%u5185%u7701%u673A%u5236%20%uFF0C%u4E5F%u5C31%u662F%u8BF4%u901A%u8FC7%u6307%u5B9Aid%u662F%u4EC0%u4E48%u7C7B%u578B%20%u54CD%u5E94%u4EC0%u4E48%u65B9%u6CD5%20%u901A%u8FC7if%u8FDB%u884C%u5224%u65AD%uFF01%0A%u5173%u4E8E%u5185%u7701%u673A%u5236%u7684%u65B9%u6CD5%uFF1A%0A%0A-%20isKindOfClass%uFF1A%u662F%u5426%u662F%u8FD9%u4E2A%u7C7B%u6216%u8005%u5B50%u7C7B%uFF08%u7C7B%u5305%u62EC%u7EE7%u627F%uFF09%0A-%20isMemberOfClass%3A%20%u662F%u5426%u662F%u8FD9%u4E2A%u7C7B%uFF08%u7C7B%u4E0D%u5305%u62EC%u7EE7%u627F%uFF09%0A-%20responsToSelector%uFF1A%u662F%u5426%u662F%u7C7B%u4E2D%u7684%u67D0%u4E2A%u65B9%u6CD5%0A-%20performSelector%3A%20%20%20%u6267%u884C%u65B9%u6CD5%0A%0A%60%60%60%20objectivec%0A%5Bobj%20performSelector%3AshootSelector%5D%3B%0A%5Bobj%20performSelector%3AshootAtSelector%20withObject%3Acoordinate%5D%3B%0A%0A%5Barray%20makeObjectsPerformSelector%3AshootSelector%5D%3B//%u8BA9%u6570%u7EC4%u6240%u6709%u5143%u7D20%u6267%u884C%0A%5Barray%20makeObjectsPerformSelector%3AshootAtSelector%20withObject%3Atarget%5D%3B%0A%60%60%60%0A%0A%0A%u534F%u8BAE%u673A%u5236%uFF1A%20%20%0A%60%60%60%20objectivec%0Aid%20%3CUIScrollViewDelegate%3E%20scrollViewDelegate%3B%0A%60%60%60%0A%u4F7F%u5B83%u80FD%u591F%u5BF9%u5C16%u62EC%u53F7%u4E2D%u7684%u5B9A%u4E49%u7684%u8FD9%u4E00%u7EC4%u65B9%u6CD5%u505A%u51FA%u56DE%u5E94%0A%0A%0A%60%60%60%20objectivec%0A@interface%20Vehicle%0A-%20%28void%29move%3B%0A@end%0A%0A@interface%20Ship%20%3A%20Vehicle%0A-%20%28void%29shoot%3B%0A@end%0A%0AShip%20*s%20%3D%20%5B%5BShip%20alloc%5D%20init%5D%3B%0A%5Bs%20shoot%5D%3B%0A%5Bs%20move%5D%3B%0A%0AVehicle%20*v%20%3D%20s%3B%0A%5Bv%20shoot%5D%20%20%23Would%20not%20crash%20at%20runtime.%20But%20have%20a%20Complier%20warning%21%0A%0Aid%0A%60%60%60%0A%0A%23%23%23%20Foundatin%20Framwork%0A%0A%23%23%23%23NSObject%0A%0A-%28id%29copy%3B%0A%u8BED%u4E49%uFF1A%u5982%u679C%u53EF%u80FD%uFF0C%u8FD4%u56DE%u8BE5%u5BF9%u8C61%u7684%u4E00%u4E2A%u4E0D%u53EF%u53D8%u526F%u672C%uFF0C%u5982%u679CNSDictionary%2CNSArray%20%u5229%u7528copy%u662F%u6B63%u786E%u7684%uFF0C%u5982%u679C%u4F20%u9012%u4E00%u4E2A%u53EF%u53D8%u7684%u6570%u7EC4%u3001%u5B57%u5178%uFF0C%u90A3%u4E48%u8FD4%u56DE%u7684%u5C31%u662F%u4E00%u4E2A%u4E0D%u53EF%u53D8%u7684%u7C7B%u3002%0A-%28id%29mutableCopy%3B%0A%u8BED%u4E49%uFF1A%u4E0D%u7BA1%u63A5%u6536%u53EF%u53D8%u6216%u8005%u4E0D%u53EF%u53D8%uFF0C%u90FD%u8FD4%u56DE%u53EF%u53D8%u7684%u3002%0A%0A%23%23%23%23NSArray%0A%0A%u4E0D%u8981%u5229%u7528%u4E0B%u9762%u7684%u65B9%u6CD5%u8FDB%u884C%60for%20in%60%u904D%u5386%3A%0A%60%60%60%20objectivec%0ANSArray%20*myArray%20%3D%20...%3B%0Afor%20%28NSString%20*string%20in%20myArray%29%7B//%u6570%u7EC4%u5143%u7D20%u53EF%u80FD%u4E0D%u5305%u542BNSString%u7C7B%u578B%0A%20%20%20%20double%20value%20%3D%20%5Bstring%20doubleValue%5D%3B%0A%20%20%20%20//%20Crash%20here%20if%20string%20is%20not%20an%20NSString%0A%7D%0A%60%60%60%0A%0A%u901A%u8FC7Introspection%u65B9%u5F0F%u8FDB%u884C%u9632%u5FA1%u5F0F%u7F16%u7A0B%uFF1A%0A%60%60%60%20objectivec%0ANSArray%20*myArray%20%3D%20...%3B%0Afor%20%28id%20obj%20in%20myArray%29%7B%0A%20%20%20%20if%28%5Bobj%20isKindOfClass%3A%5BNSString%20class%5D%5D%29%7B%0A%20%20%20%20%20%20%20%20//%20send%20NSString%20messages%20to%20obj%20with%20no%20worries.%0A%20%20%20%20%7D%0A%7D%0A%60%60%60%0A%0A%23%23%23%23NSNumber%0A%0A%u521B%u5EFANSNumber%20old%u65B9%u6CD5%uFF1A%0A%60%60%60%20objectivec%0ANSNumber%20*n%20%3D%20%5BNSNumber%20numberWithInt%3A24%5D%3B%0Afloat%20f%20%3D%20%5Bn%20floatValue%5D%3B%0A%60%60%60%0A%0A%u65B0%u7684%u8BED%u6CD5%u521B%u9020NSNumber%20in%20iOS%206%20%3A%20@%28%29%0A%60%60%60%20objectivec%0ANSNumber%20*three%20%3D%20@3%3B%0ANSNumber%20*underline%20%3D%20@%28NSUnderlineStyleSingle%29%3B%0ANSNumber%20*match%20%3D%20@%28%5Bcard%20match%3A@%5BotherCard%5D%5D%29%3B%20%20%0A%60%60%60%0A%0A%23%23%23%23%20NSDictionary%20%0A%0A%u679A%u4E3E%u904D%u5386%u7684%u65B9%u5F0F%uFF1A%0A%0A%60%60%60%20objectivec%0ANSDictionary%20*myDictionary%20%3D%20...%3B%0Afor%20%28id%20key%20in%20myDictionary%29%7B%0A%20%20%20%20//%20do%20something%20with%20key%20here%0A%20%20%20%20id%20value%20%3D%20%5BmyDictionary%20objectForKey%3Akey%5D%3B%0A%20%20%20%20//%20do%20something%20with%20value%20here%0A%7D%0A%60%60%60%0A%20%20%20%0A%0A