斯坦福iOS7公开课-Lesson1-4知识点小结(自用)

来源:互联网 发布:php 高内聚 低耦合 编辑:程序博客网 时间:2024/05/22 13:16

呐,顺一顺笔记本上的东西,白胡子大叔的一堂课信息量有点大的说;另外,主要是自己看的,所以…乱就不管我的事儿啦╮(╯▽╰)╭
…所以,正确的打开方式是Command+F。

Lesson1

MVC模式

slides上有两张图,把它们的关系说得很明白。不过我觉得MVC是一种说起来比较简单理解起来也很容易、但是真正上手做却是教科书习题般的难啊。

  1. Views do not own data they display
  2. Controllers interpret/format Model information for the view
  3. an MVC can be one of the minions of some bigger MVC

.h和.m文件

  1. .h:public API
  2. .m:private implementation
  3. name必须跟文件名相同

@property

在OC中,所有的对象都在堆中,都有指针指向(ARC)

  1. strong/weak
  2. nonatomic
  3. setter/getter method

- @synthesize:若同时修改了setter和getter方法,需重新声明@synthesize
- 重命名

Lesson2

NSMutableArray

  1. 是NSArray的子集
  2. 方法:insertObject:atIndex, addObject
  3. Adding nil to your NSMutableArray will crash your program

为对象分配空间并初始化

  1. declaring a @property: 为指针本身分配空间,但并未为其指向的object分配空间
  2. put the needed heap allocation is in the getter
  3. 所有property初始化为0(对指针来说是nil,对原始变量来说是0)
  4. Lazy instantiation

数组使用

  1. syntax sugar: self.cards[index] = [self.cards objectAtIndex:index]
  2. index = 0, 数组下标越界——>crash
  3. 良好的做法(coding style):initialize it to the default value, then set, return
  4. (Lesson3)firstObject方法比otherCards[0]好,因为后者可能越界(前者保证取出的元素不为nil?)

类方法与实例方法

  1. +: class method, no instance variables(创建事物,工具方法)
  2. -: instance method, send to instances of a class

getter/setter method

  1. 设置访问方法的名字
  2. 设置只读或只写,readonly,只有getter
  3. 语义weak, strong
  4. 在getter中,为object分配空间并初始化

instancetype: Related Result Type(相关返回类型)

  1. return an object which will be the same type as the object that this message was sent to
  2. 总是在init方法中使用(需要检验是否成功初始化父类:if(self = [super init])
  3. 对比id类型

    • 只能作为返回值
    • 对于init,alloc方法,以id作为返回类型,会返回类本身的类型,但类方法的返回类型,LLVM(Clang)无法判断,会出现错误
    • "No visible @interface for "NS…" declares the selector …"消除该错误的方法有两种:(a)显式转换(b)使用instancetype
    • 保证Complier正确推断方法返回值类型

Lesson3

readwrite

only time use: redeclare a readonly one from public to private

继承

  1. OC中没有protected,任何子类需要继承的,必须设为public
  2. 继承的方法重写,不需要添加到public API(不需要再声明一次)

designating initializer(指定初始化器)

  1. 需要调用super designating initializer
  2. 只有一个
  3. 如果该初始化器与父类不同,则需要将其标注出来

定义常量

  1. #define ……:替换
  2. static const:有类型,better in debugger
  3. @" ":创建字符串常量

outlet collection

  1. strong
  2. Complier无法知道数组中有什么
  3. order does not matter
  4. View指向array内部的东西strongly,但是并没有指向数组本身,so Controller strongly points array

Class Extension(类扩展)

@interface 类名()
//定义合成属性或方法,但不能添加字段
@end

  1. 所谓扩展,为一个类添加额外的方法或属性
  2. 扩展中,只能扩展合成方法或属性,不能添加字段
  3. 区别:(分类)在运行时添加到类中
  4. 在.m文件中声明私有方法的方式

sender

[self.cardButtons indexOfObject:sender]
//sender有序号?

Lesson4

Creating Objects

  1. alloc and init: most of time
  2. class method: +(id)stringWithFormat:(NSString*)format,…
  3. instance method: -(id)mutableCopy, -(NSString*)stringByAppendingString
  4. 返回元素:-(id)lastObject
    如果方法中没有copy,那么对一个已经存在的objects,返回的都是指针

nil

send message to nil, and method will return nil.
注意:返回struct需要小心,return value is undefined

Dynamic Binding(动态绑定)

  1. id

- is a point to an object I don't know the object
- all objects points are id:动态绑定,运行时确定
- we use static typing a lot, check at runtime
2. Static typing
- firstObject returns an id

id

  1. 什么时候使用id呢?

- array中放入不同类型的对象
- 支持blind structured communication, 用指针指向未知类型的对象(View发送action给Controller必须有指向其的指针)
2. 使用id需要保护机制
- 运行时检验id
- 使用protocols: 保证其反应的类型/方法

introspection

  1. NSObject中的方法(三种)
  2. selector方法名的识别器,SEL

- respondToSelector:@selector(method's name)//如果方法有参数加入冒号即可
- 使用SEL作为参数:performSelector, performSelector:WithObject
- 在NSArray中使用:[array makeObjectsPerformSelector:shootSelector]
- 设置action(不使用ctrl拖动)
3. 什么时候需要introspection
- 返回id类型元素的时候,需要对其进行检验和cast,保证为正确类型
- mvc中的blind structured communication

Enumeration枚举

NSNumber

  1. 包含原始类型,以放入NSArray, NSDictionary
  2. @(),@数字:返回NSNumber,括号中放入原始类型

NSValue

将struct包装起来:一个方法是,将它转化为字符串,则可以将其放入数组中,用来debug。

NSData

二进制包

NSDate

处理时间显示的格式

NSSet/NSMutableSet

  1. 无序集合,元素只有一个
  2. 用处:经过hash处理,可以用来查找

NSDictionary

  1. All values and keys are in heap(objects).
  2. 创建:@{key1:value1, ……}
  3. 查找:

    • objectForKey
    • 方括号:colors[colorString]
  4. 键值:需要hash,isEqual,故NSString通常用来作为键值
  5. 遍历方法

    for(id key in myDictionary){……}

Property List属性列表

  1. means:a collection of collections
  2. iOS中的API, 将属性列表作为参数

NSUserDefault

  1. 共享字典,甚至在程序启动和退出时仍然存在,很小
  2. 该数据库中的一切都是属性列表
  3. 访问:返回的是全局的
  4. sample methods
  5. 一旦得到这种实例,记得同步

NSRange

  1. C struct
  2. NSNotFound:NSUInteger, location
  3. NSRangePointer: NSRange*

- 在iOS中,一般不会将struct放入堆中,该指针用于引用调用range
- 如果使用NSRangePointer作为参数,使用信息填充
4. method:NSEqualRanges()

UIKit

UIColor

UIFont

  1. user content用户信息:区别text on button,etc
  2. 首选字体:样式-语义环境->用户信息
  3. 系统字体:按钮(除了自定义样式)

UIFontDescriptor

iOS7:将类别施加在未分类的字体上

Attributed Strings

  1. 除了字体本身之外,其它的一些样式信息
  2. NSAttributed String
    3.NSMutableAttributedString和NSMutableString修改会相互追踪
0 0
原创粉丝点击