您的位置:首页 > 移动开发 > Objective-C

2011斯坦福大学iOS应用开发教程学习笔记(第三课)Objective-C

2012-11-22 22:50 766 查看

第二课名称是:Objective-C

回顾上节课的内容:

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

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

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

2、为什么用.号

美观,可读性增强
可以和C语言的结构体配合
注意类型需要大写,这是个规范。3、strong VS weakstrong,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 初始化方法:
@implementation MyObject
- (id)init
{
   self = [super init]; // call our super’s designated initializer 
   if (self) {
   // initialize our subclass here
   }
   return self;
} 
@end
为什么要给self赋值呢?因为这是一种协议机制,确保super的初始化在我们之前初始化,如果super初始化失败,那就返回nil。id 不等于(void*),id是obj-c的一个内置的类型。

7、动态绑定

id类型和NSString*类型实质上没有什么区别,实质为了更好的找出语法方面的bug.在运行时发现消息都会去浔找消息的执行。例子代码:
@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方法的。

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类型
SEL shootSelector = @selector(shoot);
SEL shootAtSelector = @selector(shootAt:);
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 有序的对象集合,不可变。下面是最常用的数组的方法。
+ (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
不能把nil放到数组中。NSNull都能放进去,但是它只是个占位符。copy,可变数组返回不可变 不可变数组可以返回可变的。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;
NSMutableDicationary
+ (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;

Enumeration
NSSet *mySet = ...;
for (id obj in mySet) {
if ([obj isKindOfClass:[NSString class]]) {
}

NSDictionary *myDictionary = ...;
   for (id key in myDictionary) {
    // do something with key here
    id value = [myDictionary objectForKey:key];
// do something with value here 
   }

10、property List

NSArray, NSDictionary, NSNumber, NSString, NSDate, NSData 这6中是property List11、NSUserDefaults是轻量级的property List存储。通过standardUserDefaults方法来存取。
常用方法
- (void)setDouble:(double)aDouble forKey:(NSString *)key;
- (NSInteger)integerForKey:(NSString *)key; // NSInteger is a typedef to 32 or 64 bit int
- (void)setObject:(id)obj forKey:(NSString *)key; // obj must be a Property List
- (NSArray *)arrayForKey:(NSString *)key; // will return nil if value for key is not
用[[NSUserDefaults standardUserDefaults] synchronize];方法来同步到去存储,任何操作后都要存储一下,开销不大。

不得不说,课程的信息量很大。
容芳志 (http://blog.csdn.net/totogo2010)本文遵循“署名-非商业用途-保持一致”创作公用协议
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐