上节课内容
- 创建了单个MVC模式的项目
- 显示项目的各个文件,显示或隐藏导航,Assistant Editor, Console, Object Library, Inspector等功能的使用
- 在故事版上编辑视图,通过Ctrl+拖拽把view连接到Controller的outlet。
- 创建新的类,比如 CalculatorBrain
- 使用@synthesize
- 延迟实例化实现
- getter[ ]中括号的使用
- 私有方法在.m文件中定义
- 使用strong weak属性处理代码中的警告和错误
- 相关Obj-c的语法知识,比如NSString 的使用
1. 为什么使用@property
- 为实体变量提供安全性和继承能力
- 提供延迟实例化,如:UI更新,一致性检测。
property也可以没有实例变量,不使用@synthesize就可以。同时,也可以有实例变量而没有@property, 但是目前记住使用@property就可以。
2. 为什么使用.号
- 美观,可读性增强
- 和C语言的结构体中成员变量访问类似
3. strong VS weak
strong 和 weak的概念是针对@property对象而言的。
strong, weak 都是指针的属性。
- strong: 只要指针指向那块内存,就不能被释放。除非指针被置为nil,或者对象本身被从heap移除。
- weak: 指针指向的内存必须还有其他strong指针指向才被保留,否则立即将weak指针置为nil。
strong 和weak 概念不是垃圾收集机制,没有strong 指针指向的对象会被立即清除。垃圾收集机制有delay。
4. nil
nil是什么也不指向的指针的值
- nil 就是0
- 给nil发送消息是ok的,什么都不会执行
- 所有的实例变量初始值都是0,因此指针适量变量的初始值是nil
5. BOOL
bool实际上是typedef, 不能使用小写。
6. 实例和类方法
实例方法:
- 以 '-'开头
- 调用:[<pointer to instance> method]
- self/super调用的时候: self指自己的实现,super指父类的实现
类方法:
- 以'+' 开头
- 通常用来创建或者作为工具方法
- 调用:[Class method]或者简介通过实例的'class'方法
- self/super调用的时候: self意味着这个类的类方法,super意味着父类的类方法
7. 实例化
- 通过其他对象创建对象(通过方法)
- (NSString *) stringByAppendingString:(NSString *) otherString; //NSString
- (id)mutableCopy; // NSString & NSArray
- (NSString *)componentsJoinedByString:(NSString *)separator; // NSArray
- 由其他对象得来的对象并不都是新创建的
- 使用类方法创建对象
+(id)stringWithFormat:(NSString *)format, // NSString
+(id) buttonWithType:(UIButtonType)buttonType; // UIButton
+(id) arrayWithObject:(id)anObject; //NSArray
实例初始化的时候要先调用父类的初始化方法:
- (id) init
{
self = [super init];
if(self){
// initialize our subclass here
}
return self;
}
8. 动态绑定
消息的执行代码是在运行时刻决定的而不是编译时刻。
@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];
当调用给v 发送shoot的消息时,虽然Vehicle没有shoot方法,但是程序不会崩溃,编译器会给个警告而已,运行时会找到v其实时有shoot方法的。
9. 内省
内省(Introspection)是面向对象语言和环境的一个强大特性,Objective-C和Cocoa在这个方面尤其的丰富。内省是对象揭示自己作为一个运行时对象的详细信息的一种能力。这些详细信息包括对象在继承树上的位置,对象是否遵循特定的协议,以及是否可以响应特定的消息。NSObject协议和类定义了很多内省方法,用于查询运行时信息,以便根据对象的特征进行识别。
明智地使用内省可以使面向对象的程序更加高效和强壮。它有助于避免错误地进行消息派发、错误地假设对象相等、以及类似的问题。
所有继承自NSobject的对象都有三个方法:
isKindOfClass: 对象是否属于某一类(包括父类)
isMemberOfClass: 对象是否是某个类(不包括父类)
respondsToSelector: 对象是否对某个方法作出响应
通过这三个方法可以实现内省机制, 如isKindOfClass。
if([obj isKindOfClass: [NSString class]]){
NSString *s = [(NSString *obj) stringByAppendingString:@"xyzzy"];
}
重点介绍下SEL:
SEL是objective-c中selector的类型:
SEL shootSelector = @selector(shoot) // shoot方法选择器
SEL shootAtSelector = @selector(shootAt)
有了SEL之后可以让对象或者对象数组执行 selector。
对象, performeSelector():
[obj performSelector:shootSelector];
[obj performSelector:shootAtSelector];
对象数组,makeObjectsPerformSelector:
[array makeObjectsPerformSelector:shootSelector];
[array makeObjectsPerformSelector:shootAtSelector withObject:target];
10. Foundation 框架
-
NSObject
基本上是IOS SDK中所有类的基类, 实现了内省方法等。 -
NSString
可以是Unicode编码的任意字符串,使用@"", 不可变。NSString已经优化的性能非常的好了,最好不要使用MutableString。 -
NSMutableString:
NSString的可变版本,很少使用。 -
NSNumber
对原始int float double 数据类型的封装。 -
NSValue
封装非对象数据 -
NSData
二进制数据对象,用来在IOS SDK之间存储/恢复/传输数据。 -
NSDate
日期数据 -
NSArray
有序的对象数组,不可变。
+ (id)arrayWithObjects:(id)firstObject, ...; // nil-terminated arguments
NSArray *primaryColors = [NSArray arrayWithObjects:@“red”, @“yellow”, @“blue”, nil];
+ (id)arrayWithObject:(id)soleObjectInTheArray; // more useful than you might think!
- (int)count;
- (id)objectAtIndex:(int)index;
- (id)lastObject; // returns nil (doesn’t crash) if there are no objects in the array
- (NSArray *)sortedArrayUsingSelector:(SEL)aSelector;
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)selectorArgument;
- (NSString *)componentsJoinedByString:(NSString *)separator;
- (BOOL)containsObject:(id)anObject; // could be slow, think about NSOrderedSet
- NSMutableArray
可变对象数据
+ (id)arrayWithCapacity:(int)initialSpace; // initialSpace is a performance hint only
+ (id)array;
- (void)addObject:(id)anObject; // at the end of the array
- (void)insertObject:(id)anObject atIndex:(int)index;
- (void)removeObjectAtIndex:(int)index;
- (void)removeLastObject;
- (id)copy;
- NSDictionary()
+ (id)dictionaryWithObjects:(NSArray *)values forKeys:(NSArray *)keys;
+ (id)dictionaryWithObjectsAndKeys:(id)firstObject, ...;
NSDictionary *base = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:2],
@“binary”,[NSNumber numberWithInt:16], @“hexadecimal”, nil];
- (int)count;
- (id)objectForKey:(id)key;
- (NSArray *)allKeys;- (NSArray *)allValues;
- NSMutableDictionary
+ (id)dictionary; // creates an empty dictionary (don’t forget it inherits + methods from super)
- (void)setObject:(id)anObject forKey:(id)key;
- (void)removeObjectForKey:(id)key;- (void)removeAllObjects;
- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;
- NSSet
不可变无序的唯一对象集合
+ (id)setWithObjects:(id)firstObject, ...;
+ (id)setWithArray:(NSArray *)anArray;- (int)count;
- (BOOL)containsObject:(id)anObject;- (id)anyObject;
- (void)makeObjectsPerformSelector:(SEL)aSelector;
- NSMutableSet
- (void)addObject:(id)anObject; // does nothing if object that isEqual:anObject is already in
- (void)removeObject:(id)anObject;
- (void)unionSet:(NSSet *)otherSet;
- (void)minusSet:(NSSet *)otherSet;- (void)intersectSet:(NSSet *)otherSet;
- NSOrderSet
NSArray和NSSet的合体,比NSSet快。
- (int)indexOfObject:(id)anObject;- (id)objectAtIndex:(int)anIndex;
- (id)firstObject; and - (id)lastObject; - (NSArray *)array;
- (NSSet *)set;
- NSMutableOrderSet
- (void)insertObject:(id)anObject atIndex:(int)anIndex;
- (void)removeObject:(id)anObject;
- (void)setObject:(id)anObject atIndex:(int)anIndex;
11. enumeration
快速遍历集合中对象的方法
NSSet *mySet = ...;
for (id obj in mySet) {
if ([obj isKindOfClass:[NSString class]]) {
}
12. property list
property list是集合的集合
NSArray, NSDictionary, NSNumber, NSString, NSDate, NSData 只有这6种是property List
网友评论