第二课名称是:Objective-C

回顾上节课的内容:

  1. 创建了单个MVC模式的项目
  2. 显示项目的各个文件,显示或隐藏导航,Assistant Editor, Console, Object Library, Inspector等功能的使用
  3. 在故事版上编辑视图,通过Ctrl+拖拽把view连接到Controller的outlet。
  4. 创建新的类,比如 CalculatorBrain
  5. 使用@synthesize
  6. 延迟实例化实现getter
  7. [ ]中括号的使用
  8. 私有方法在.m文件中定义
  9. 使用strong weak属性
  10. 处理代码中的警告和错误
  11. 相关Obj-c的语法知识,比如NSString 的使用
这节课主要是讲Obj-C语法,实例化初始化,内省,Foundation框架里的主要的一些类的使用

1、为什么用property,理由有两个:

  • 实体变量的安全性和继承能力
  • 提供延迟实例化,比如:UI更新,一次性检测。
property可以没有实体变量,怎么做到的呢?
不要用@synthesize,自己创建getter 和setter.
反过来,也可以有实体变量,没有property。不过建议使用property。

2、为什么用.号

  • 美观,可读性增强
  • 可以和C语言的结构体配合
注意类型需要大写,这是个规范。
3、strong VS  weak
strong,weak都是指针的属性,
strong 是只要指向那块内存,就不能释放。
weak  是内存有strong指向的时候才被保留,没strong的指向weak也会被置为nil。
weak在iOS 5才能使用。
这是引用计数技术,不是垃圾回收。当失去所有的strong的指向时,立马释放内存。strong weak是针对property的,本地变量都是strong的。
 

4、 nil =0.

给nil发送消息,也是ok的。
BOOL 类型:YES  NO;
不能用小写的bool。

5、类方法和实例方法

+号是类方法
-号是实例方法
方法的参数识别:带星号的,是类指针变量,内容在堆上,不带星号的是变量在栈上。

6、实例化

通过其他对象创建对象。
通过alloc 和init创建对象
alloc是NSObject的类方法,alloc是不够的,还要初始化,必须要初始化。
可以自定义很多的init方法,NSString 有实际中init方法比如:
- (id)initWithCharacters:(const unichar *)characters length:(int)length; 
- (id)initWithFormat:(NSString *)format, ...;
- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding;
初始化时,要先调用父类的初始化方法。
要指定初始化方法。防治循环调用。
数组的指针都是strong 
初始化方法:
  1. @implementation MyObject
  2. - (id)init
  3. {
  4. self = [super init]; // call our super’s designated initializer
  5. if (self) {
  6. // initialize our subclass here
  7. }
  8. return self;
  9. }
  10. @end

为什么要给self赋值呢?因为这是一种协议机制,确保super的初始化在我们之前初始化,如果super初始化失败,那就返回nil。

id 不等于(void*),id是obj-c的一个内置的类型。

7、动态绑定

id类型和NSString*类型实质上没有什么区别,实质为了更好的找出语法方面的bug.在运行时发现消息都会去浔找消息的执行。
例子代码:
  1. @interface Vehicle
  2. - (void)move;
  3. @end
  4. @interface Ship : Vehicle
  5. - (void)shoot;
  6. @end
  7. Ship *s = [[Ship alloc] init];
  8. [s shoot];
  9. [s move];
  10. Vehicle *v = s;
  11. [v shoot];

当调用给v 发送shoot的消息时,虽然Vehicle没有shoot方法,但是程序不会崩溃,编译器会给个警告而已,运行时会找到v其实时有shoot方法的。

 

8、内省

id可以让数组里存入各种类型的对象。
如何知道id的类呢?
isKindOfClass: returns whether an object is that kind of class (inheritance included) 
isMemberOfClass: returns whether an object is that kind of class (no inheritance) 
respondsToSelector: returns whether an object responds to a given method
 
SEL类型
  1. SEL shootSelector = @selector(shoot);
  2. SEL shootAtSelector = @selector(shootAt:);
  3. SEL moveToSelector = @selector(moveTo:withPenColor:);
[obj performSelector:shootSelector]; 无参数的SEL
[obj performSelector:shootAtSelector withObject:coordinate];有一个参数的SEL。
 

9、foundation 框架

NSObject的方法
-(NSString*)description ,用在NSLog,%@。
NSString对象
NSString 对Unicode编码的任意语言的字符串,可以容纳任何语言。用@""编译成NSString 
NSString是不可变的。会返回新的字符串。NSString的使用方法太多了,建议查看文档使用。
NSString已经优化的性能非常的好了,最好不要使用MutableString。
 
NSNumber 封装原始数据比如 Int float  double等。
NSValue 封装非对象的数据
NSData 二进制
NSDate  日历
NSArray 有序的对象集合,不可变。下面是最常用的数组的方法。
  1. + (id)arrayWithObjects:(id)firstObject, ...; // nil-terminated arguments
  2. NSArray *primaryColors = [NSArray arrayWithObjects:@“red”, @“yellow”, @“blue”, nil];
  3. + (id)arrayWithObject:(id)soleObjectInTheArray; // more useful than you might think!
  4. - (int)count;
  5. - (id)objectAtIndex:(int)index;
  6. - (id)lastObject; // returns nil (doesn’t crash) if there are no objects in the array
  7. - (NSArray *)sortedArrayUsingSelector:(SEL)aSelector;
  8. - (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)selectorArgument;
  9. - (NSString *)componentsJoinedByString:(NSString *)separator;
  10. - (BOOL)containsObject:(id)anObject; // could be slow, think about NSOrderedSet

不能把nil放到数组中。NSNull都能放进去,但是它只是个占位符。

copy,可变数组返回不可变
             不可变数组可以返回可变的。
NSMutableArray
  1. + (id)arrayWithCapacity:(int)initialSpace; // initialSpace is a performance hint only + (id)array;
  2. - (void)addObject:(id)anObject; // at the end of the array - (void)insertObject:(id)anObject atIndex:(int)index;
  3. - (void)removeObjectAtIndex:(int)index;
  4. - (void)removeLastObject;
  5. - (id)copy;
可变继承了不可变。
 
NSDictionary类
  1. + (id)dictionaryWithObjects:(NSArray *)values forKeys:(NSArray *)keys;
  2. + (id)dictionaryWithObjectsAndKeys:(id)firstObject, ...;
  3. NSDictionary *base = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:2], @“binary”,
  4. [NSNumber numberWithInt:16], @“hexadecimal”, nil];
  5. - (int)count;
  6. - (id)objectForKey:(id)key;
  7. - (NSArray *)allKeys;
  8. - (NSArray *)allValues;

NSMutableDicationary

  1. + (id)dictionary; // creates an empty dictionary (don’t forget it inherits + methods from super)
  2. - (void)setObject:(id)anObject forKey:(id)key;
  3. - (void)removeObjectForKey:(id)key;
  4. - (void)removeAllObjects;
  5. - (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;
 
NSSet 不可变无序的唯一对象集合
  1. + (id)setWithObjects:(id)firstObject, ...;
  2. + (id)setWithArray:(NSArray *)anArray;
  3. - (int)count;
  4. - (BOOL)containsObject:(id)anObject;
  5. - (id)anyObject;
  6. - (void)makeObjectsPerformSelector:(SEL)aSelector;
NSMutableSet
  1. - (void)addObject:(id)anObject; // does nothing if object that isEqual:anObject is already in - (void)removeObject:(id)anObject;
  2. - (void)unionSet:(NSSet *)otherSet;
  3. - (void)minusSet:(NSSet *)otherSet;
  4. - (void)intersectSet:(NSSet *)otherSet;

NSOrderSet。是NSArray和NSSet的合体,比NSSet快。

  1. - (int)indexOfObject:(id)anObject;
  2. - (id)objectAtIndex:(int)anIndex;
  3. - (id)firstObject; and - (id)lastObject; - (NSArray *)array;
  4. - (NSSet *)set;

NSMutableOrderSet

  1. - (void)insertObject:(id)anObject atIndex:(int)anIndex;
  2. - (void)removeObject:(id)anObject;
  3. - (void)setObject:(id)anObject atIndex:(int)anIndex;
 

Enumeration

  1. NSSet *mySet = ...;
  2. for (id obj in mySet) {
  3. if ([obj isKindOfClass:[NSString class]]) {
  4. }
  1. NSDictionary *myDictionary = ...;
  2. for (id key in myDictionary) {
  3. // do something with key here
  4. id value = [myDictionary objectForKey:key];
  5. // do something with value here
  6. }

10、property List

NSArray, NSDictionary, NSNumber, NSString, NSDate, NSData 这6中是property List
11、NSUserDefaults是轻量级的property List存储。
通过standardUserDefaults方法来存取。
 
常用方法
  1. - (void)setDouble:(double)aDouble forKey:(NSString *)key;
  2. - (NSInteger)integerForKey:(NSString *)key; // NSInteger is a typedef to 32 or 64 bit int 
  1. - (void)setObject:(id)obj forKey:(NSString *)key; // obj must be a Property List
  2. - (NSArray *)arrayForKey:(NSString *)key; // will return nil if value for key is not

[[NSUserDefaults standardUserDefaults] synchronize];方法来同步到去存储,任何操作后都要存储一下,开销不大。

2011年冬斯坦福大学公开课 iOS应用开发教程学习笔记(第三课)的更多相关文章

  1. 2011斯坦福大学iOS应用开发教程学习笔记(第一课)MVC.and.Introduction.to.Objective-C

    blog.csdn.net/totogo2010/article/details/8205810  目录(?)[-] 第一课名称 MVC and Introduction to Objective-C ...

  2. Stanford公开课《编译原理》学习笔记(1~4课)

    目录 一. 编译的基本流程 二. Lexical Analysis(词法分析阶段) 2.1 Lexical Specification(分词原则) 2.2 Finite Automata (典型分词算 ...

  3. 斯坦福大学公开课:iOS 7应用开发 笔记

    2015-07-06 第一讲   课务.iOS概述 -------------------------------------------------- 开始学习斯坦福大学公开课:iOS 7应用开发留 ...

  4. 第19月第8天 斯坦福大学公开课机器学习 (吴恩达 Andrew Ng)

    1.斯坦福大学公开课机器学习 (吴恩达 Andrew Ng) http://open.163.com/special/opencourse/machinelearning.html 笔记 http:/ ...

  5. 斯坦福大学公开课机器学习:advice for applying machine learning | diagnosing bias vs. variance(机器学习:诊断偏差和方差问题)

    当我们运行一个学习算法时,如果这个算法的表现不理想,那么有两种原因导致:要么偏差比较大.要么方差比较大.换句话说,要么是欠拟合.要么是过拟合.那么这两种情况,哪个和偏差有关.哪个和方差有关,或者是不是 ...

  6. Stanford公开课《编译原理》学习笔记(2)递归下降法

    目录 一. Parse阶段 CFG Recursive Descent(递归下降遍历) 二. 递归下降遍历 2.1 预备知识 2.2 多行语句的处理思路 2.3 简易的文法定义 2.4 文法产生式的代 ...

  7. 斯坦福大学公开课机器学习: machine learning system design | error analysis(误差分析:检验算法是否有高偏差和高方差)

    误差分析可以更系统地做出决定.如果你准备研究机器学习的东西或者构造机器学习应用程序,最好的实践方法不是建立一个非常复杂的系统.拥有多么复杂的变量,而是构建一个简单的算法.这样你可以很快地实现它.研究机 ...

  8. 斯坦福大学公开课机器学习:machine learning system design | data for machine learning(数据量很大时,学习算法表现比较好的原理)

    下图为四种不同算法应用在不同大小数据量时的表现,可以看出,随着数据量的增大,算法的表现趋于接近.即不管多么糟糕的算法,数据量非常大的时候,算法表现也可以很好. 数据量很大时,学习算法表现比较好的原理: ...

  9. 斯坦福大学公开课机器学习:machine learning system design | trading off precision and recall(F score公式的提出:学习算法中如何平衡(取舍)查准率和召回率的数值)

    一般来说,召回率和查准率的关系如下:1.如果需要很高的置信度的话,查准率会很高,相应的召回率很低:2.如果需要避免假阴性的话,召回率会很高,查准率会很低.下图右边显示的是召回率和查准率在一个学习算法中 ...

随机推荐

  1. 超烂的ELK之filebeat读取【已解决】

    搞了无数次的filebeat-->logstash今天栽了跟头 filebeat在读取如下文件的时候,openchgw.log 软连接speechgw.log.20170703183729文件 ...

  2. Android 启动后台运行程序(Service)

    Android开发中,当需要创建在后台运行的程序的时候,就要使用到Service.Service 可以分为有无限生命和有限生命两种.特别需要注意的是Service跟Activities是不同的(简单来 ...

  3. Android问题-“signaturs do not match the previously installed version”

    问题现象:电脑上的XE10.2中写代码,F9后,提示“signaturs do not match the previously installed version;” 问题原因:签名与以前安装的版本 ...

  4. WPF 冒泡路由事件

    在WPF中,例如,可以构建一个包含图形的按钮,创建一个具有文本和图片混合内容的标签,或者为了实现滚动或折叠的显示效果在一个特定的容器中放置内容.甚至可以多此重复嵌套,直到达到您所希望的层次深度. 这种 ...

  5. 关于pthread_cond_wait使用while循环判断的理解

    在Stevens的<Unix 环境高级编程>中第11章线程关于pthread_cond_wait的介绍中有一个生产者-消费者的例子P311,在进入pthread_cond_wait前使用w ...

  6. 设计模式-观察者模式(上)<转>

    本文参考Head First设计模式一书,感觉书中的例子实在很好,很贴切.对模式的知识点进行总结,并对书的源码做了一定注释.   观察者模式要点有二:主题和观察者. 最贴切的案例是:杂志订阅,杂志是主 ...

  7. C++中的类继承之单继承&多继承&菱形继承

     C++中的类继承之单继承&多继承&菱形继承 单继承是一般的单一继承,一个子类只 有一个直接父类时称这个继承关系为单继承.这种关系比较简单是一对一的关系: 多继承是指 一个子类有两个或 ...

  8. Hadoop Balancer源代码解读

    前言 近期在做一些Hadoop运维的相关工作,发现了一个有趣的问题,我们公司的Hadoop集群磁盘占比数值參差不齐,高的接近80%.低的接近40%.并没有充分利用好上面的资源,可是balance的操作 ...

  9. 轻量级ORM框架Dapper应用二:使用Dapper实现CURD操作

    在上一篇文章中,讲解了如何安装Dapper,这篇文章中将会讲解如何使用Dapper使用CURD操作. 例子中使用到的实体类定义如下: using System; using System.Collec ...

  10. [mysql] mysql-myibatis-整理

    ==================================== insert ========================================== 语句1 <inser ...