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中的动态类型相关 ...
随机推荐
- poj2443(简单的状态压缩)
POJ2443 Set Operation Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 2679 Accepted: ...
- ACM核武器
工欲善其事必先利其器,给大家介绍一下ACM里面经常使用的一些工具,平台,作为第一发福利. 详细看这里,我直接粘贴过来有些代码没贴过来 http://wuyiqi.net/house/acm_weap ...
- activiti入门2流程引擎API和服务基础设施
RepositoryService : 管理和控制公布包和流程定义(包括了一个流程每一个环节的结构和行为)的操作 除此之外,服务能够 查询引擎中的公布包和流程定义. 暂停或激活公布包.相应所有和特定流 ...
- Layout Renderers
Layout Renderers NLog package ${activityid} - Puts into log a System.Diagnostics trace correlation i ...
- 利用BBED恢复UPDATE改动前的值
转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/30615151 实验步骤例如以下: 1.创建表guo_test1 gyj@PROD> ...
- Android环境结构--安装Eclipse错
在学习安卓第一步.成立了一个开发环境. 经验,知道,所以这一步是不容易,因为你觉得,我可能是太幸运了. 我见到 题. 首先,安装Eclipse的时候. [Problem 1] [问题原因]: (1) ...
- HDU 3639 Hawk-and-Chicken(Tarjan缩点+反向DFS)
Problem Description Kids in kindergarten enjoy playing a game called Hawk-and-Chicken. But there alw ...
- java界面编程(8) ------ 组合框(下拉列表)
本文是自己学习所做笔记,欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020 与一组单选button的功能类似,组合框(下拉列表)也是强制用户从一组可能的元素 ...
- BAT 特殊符号总结
原文:BAT 特殊符号总结 BAT特殊符号总结,用好特殊符号,利用提高开发效率.^ 转义符 用在特殊符号之前 比如: echo 非常^&批处理 如果不加^ 那么"批处理"将 ...
- sql 给表结构增加说明
create proc proc_addReMark @TableName nvarchar(50), @RowName nvarchar(50), @RowReMark nv ...