PART II —— Foundation Framework
Chapter 14-15
Introduction
- a framework is a collection of classes, methods, functions and documentation logically grouped together to make developing programs easier.
- Foundation framework —— provides the base of foundation for all your program development:numbers, strings, arrays, dictionaries, and sets
- Application Kit framework —— an extensive collection of classes and methods to develop interactive graphical applications:texts, menus, toolbars, tables, …
- Cocoa (OS X)环境包括Foundation framework、the Application Kit framework 和 Core Data
- Cocoa Touch (iOS)环境包括Foundation、Core Data 和 UIKit framework
- 官方提供的Foundation Documentation
数字对象 — NSNumber
- Xcode 5 新特性——“modules” can help speed up compilation time as well as help to avoid name conflicts between different libraries
- Modules相当于将框架进行了封装,然后加入在实际编译之时加入了一个用来存放已编译添加过的Modules列表1。
- All number objects must be newly created. You cannot change the value of a previously created NSNumber object. immutable
- numberWithInt: → intValue → %i
- numberWithInteger: → integerValue → %li
字符串对象 — NSString
- objects(NSString….) → %@
- description方法:使用%@格式化输出自己写的class的对象;默认显示class名和objects的内存地址,override就可以修改其显示内容 → good debugging tool
- NSString对象 → immutable
- 但是指向对象的引用可以更改
- NSMutableString对象 → mutable
- stringWithString: → 复制字符串的内容,创建新的NSString对象 ; “=” →复制指向对象的引用,不创建新对象
- caseInsensitiveCompare: → 不区分大小写的比较
- NSRange
- 一个C结构体的别名,包含location和length两个元素(类型NSUInteger)
- 字符串中的location从0开始
- 创建一个NSRange:NSMakeRange(location, length)
数组对象 — NSArray
- an ordered collection of objects
immutable (NSArray) and mutable (NSMutableArray, subclass of NSArray)
[NSArray arrayWithObjects: elem1,…,nil];
等价于@[elem1, elem2, …]
array[index]
等价于[array objectAtIndex:index]
array[index] = object
等价于[array setObject: object forIndex: index]
for each element in the array, NSLog uses the description method from the class the element belongs to
- 利用复杂算法处理大量的数据数组,c语言的方法更好
- property的attributes (copy, nonatomic,…)
- copy:make a copy of the instance variable in the setter method;default是not copy
- nonatomic:don’t need to worry about race conditions that could occur from multiple threads trying to access the instance variable at the same time
- strong:make an additional reference to the object whenever the setter method is used. Influence the life span of the object.
- 初始化(init,initWith……)方法的返回值类型使用instancetype,便于其subclass使用:编译器会根据receiver的类型确定方法的返回值类型
- fast enumeration :for(variable in array){……}
- enumerateObjectUsingBlock
- identical object → objects with the same location in memory
- NSComparator →
typedef NSComparisonResult (^NSComparator)(id obj1, id obj2);
- NSComparator is a block that takes two objects as its arguments and that returns a value of type NSComparisonResult.
- 使用block可以提速(多线程)
- don’t have to add a comparison method to the class whose objects are being compared
- if you want to change the way they are compared, you can do it directly in the method (block), which means you don’t have to change original class.
- NSValue class : 结构体 →wrap→ 对象;对象 →unwrap→ 结构体
- CGPoint, CGSize, CGRect 等是结构体而非对象,不能直接放入array中
字典对象 — NSDictionary
- key must be unique, but can be of any type object; often is string
value can also be of any type object, but not nil.
dict[key]
等价于[dict objectForKey: key]
dict[key] = object
等价于[dict setObject: object forKey: key]
Set Objects
- a collection of unique object
- NSSet, NSMutableSet, NSIndexSet, NSCountedSet, …
- 包含的一些操作:
- searching, adding, and removing members (mutable sets); comparing two sets; and finding the intersection(交集) and union(并集) of two sets.
- NSIndexSet :store ordered indexes into some other data structure, typically an array
- 只有immutable,没有mutable的