CS193p 斯坦福IOS开发 2011 (三)

2018-10-10  本文已影响0人  ItchyHiker

上节课内容

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

1. 为什么使用@property

2. 为什么使用.号

3. strong VS weak

strong 和 weak的概念是针对@property对象而言的。
strong, weak 都是指针的属性。

strong 和weak 概念不是垃圾收集机制,没有strong 指针指向的对象会被立即清除。垃圾收集机制有delay。

4. nil

nil是什么也不指向的指针的值

5. BOOL

bool实际上是typedef, 不能使用小写。

6. 实例和类方法

实例方法:

类方法:

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 框架

+ (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
+ (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; 
+ (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;
+ (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;
+ (id)setWithObjects:(id)firstObject, ...;
+ (id)setWithArray:(NSArray *)anArray;- (int)count;
- (BOOL)containsObject:(id)anObject;- (id)anyObject;
- (void)makeObjectsPerformSelector:(SEL)aSelector;
- (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;
- (int)indexOfObject:(id)anObject;- (id)objectAtIndex:(int)anIndex;
- (id)firstObject; and - (id)lastObject; - (NSArray *)array;
- (NSSet *)set;
- (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

上一篇 下一篇

猜你喜欢

热点阅读