Foundation 学习笔记
笔记内容
学习笔记-段玉磊
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 = [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
创建NSNumber old方法:
NSNumber *n = [NSNumber numberWithInt:];
float f = [n floatValue];
新的语法创造NSNumber in iOS 6 : @()
NSNumber *three = @;
NSNumber *underline = @(NSUnderlineStyleSingle);
NSNumber *match = @([card match:@[otherCard]]);
NSDictionary
枚举遍历的方式:
NSDictionary *myDictionary = ...;
for (id key in myDictionary){
// do something with key here
id value = [myDictionary objectForKey:key];
// 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
Foundation 学习笔记的更多相关文章
- Foundation学习笔记
. 链接:Foundation学习,代码实例总结(pdf版) .
- 深度学习笔记 (一) 卷积神经网络基础 (Foundation of Convolutional Neural Networks)
一.卷积 卷积神经网络(Convolutional Neural Networks)是一种在空间上共享参数的神经网络.使用数层卷积,而不是数层的矩阵相乘.在图像的处理过程中,每一张图片都可以看成一张“ ...
- (学习笔记)laravel 中间件
(学习笔记)laravel 中间件 laravel的请求在进入逻辑处理之前会通过http中间件进行处理. 也就是说http请求的逻辑是这样的: 建立中间件 首先,通过Artisan命令建立一个中间件. ...
- 【swift学习笔记】二.页面转跳数据回传
上一篇我们介绍了页面转跳:[swift学习笔记]一.页面转跳的条件判断和传值 这一篇说一下如何把数据回传回父页面,如下图所示,这个例子很简单,只是把传过去的数据加上了"回传"两个字 ...
- NSURLSession学习笔记
NSURLSession学习笔记(一)简介 一.URL Session的基本概念 1.三种工作模式: 默认会话模式(default):工作模式类似于原来的NSURLConnection,使用的是基于磁 ...
- Object C学习笔记22-#define 用法
上一篇讲到了typedef 关键字的使用,可以参考文章 Object C 学习笔记--typedef用法 .而在c中还有另外一个很重要的关键字#define. 一. #define 简介 在C中利用预 ...
- Object C学习笔记21-typedef用法
在上一章的学习过程中遇到了一个关键字typedef,这个关键字是C语言中的关键字,因为Object C是C的扩展同样也是支持typedef的. 一. 基本作用 typedef是C中的关键字,它的主要作 ...
- Object C学习笔记18-SEL,@ selector,Class,@class
本章是对上一章<<Object C学习笔记17-动态判断和选择器>>的一点补充,所以比较简单点. 一. SEL 类型 在上一篇介绍了几个方法,都只是介绍了其使用方式但是没有具体 ...
- Object C学习笔记17-动态判断和选择器
当时学习Object C的时被人鄙视了一顿,说使用.NET的思想来学Object C就是狗屎:不过也挺感谢这位仁兄的,这让我学习的时候更加的谨慎.今天的学习笔记主要记录Object C中的动态类型相关 ...
随机推荐
- 【关节点+桥】关节点和桥模板 Tarjan
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; con ...
- c++界面设计皮肤工具
1. 先进行软件界面设计,确定软件界面美术包装方案 2. 依据软件界面美术设计装方案制作对应的图片,图片格式请參见AUDK的帮助文档或皮肤工具SkinTool 的 Demo 3. 打开皮肤工具 Ski ...
- 【剑指offer】堆栈推弹出序列
转载请注明出处:http://blog.csdn.net/ns_code/article/details/26077863 剑指offer上的第22题,九度OJ上AC. 题目描写叙述: 输入两个整数序 ...
- Topshelf创建Windows服务
使用Topshelf创建Windows服务 概述 Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps ...
- bnu 34986 Football on Table(数学+暴力)
pid=34986" target="_blank" style="">题目连接:bnu 34986 Football on Table 题目大 ...
- div元素上下左右居中
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 找呀志_使用SQLiteDatabase增删改提供的搜索方法和事务
知识具体解释:http://blog.csdn.net/zhaoyazhi2129/article/details/9026093 MainActivity.java,User.java,BaseDa ...
- POJ2771_Guardian of Decency(二分图/最大独立集=N-最大匹配)
解决报告 http://blog.csdn.net/juncoder/article/details/38159017 题目传送门 题意: 看到题目我就笑了.., 老师觉得这种两个学生不是一对: 身高 ...
- 使用Visual Studio创建映像向导(Image Sprite)——Web Essential
原版的:Creating Image Sprite in Visual Studio - Web Essential 译者注:有关图片精灵的信息请參阅http://baike.baidu.com/vi ...
- 如何完成Nexus 9上电后激活过程
所述被激活,因为它是Nexus 9经过努力获得启动OTA最新更新包,而且因为Google关闭一堵墙.原因无法下载更新包. 因为是第一次开机,它不能被设置usb debugging, 无法adb去杀死w ...