斯坦福大学iOS应用开发教程学习笔记(第三课) Objective-C
2015-08-31 21:38
736 查看
1、为什么用property,理由有两个:
实体变量的安全性和继承能力提供延迟实例化,比如:UI更新,一次性检测。
1.1 property可以没有实体变量,怎么做到的呢? 不要用@synthesize,自己创建getter 和setter.
1.2 反过来,也可以有实体变量,没有property。不过建议使用property。
2、为什么用.号
美观,可读性增强,[ ]多难看可以和C语言的结构体配合
注意类型需要大写(类,结构体),这是个规范,其他的不用大写
3、strong VS weak
strong,weak都是指针的属性strong 是只要指向那块内存,就不能释放,一直保存内存在堆中,除非不指向任何。
weak 是内存有strong指向的时候才被保留,没strong的指向weak也会被置为nil。
weak在iOS 5才能使用。
这是引用计数技术,不是垃圾回收。当失去所有的strong的指向时,立马释放内存,是可控的。
strong weak是针对property的,本地变量都是strong的。
release是ios4的东西,后面都不用了。不要用release,retain或者其他管理内存的东西,ios已经做好了
4、nil
4.1 nil就是不指向任何东西,表示0位,所有synthesize生成的初始值都是0给nil发送消息,也是ok的,什么也不会执行。
4.2 BOOL 类型:YES NO;不能用小写的bool。
5、类方法和实例方法
+号 是类方法,对象是类,[后面是一个类,self不是实例,是个类创建一个实例,或者共享一个实例,使用。
类不是实例,没办法保存任何东西,无property或者实例变量
-号 是实例方法,对象是实例对象,
方法的参数识别:带星号的,是类指针变量,内容在堆上; 不带星号的是变量在栈上。
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;
初始化时,要先调用父类的初始化方法,在子类里面初始化
这些init方法返回类型不固定,为了继承原因写成id。
要指定一个初始化方法。防治循环调用。
数组的指针都是strong
初始化不常用,因为有property,初始化方法:
@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的一个内置的类型,编译器知道id的确切类型。
7、动态绑定
id类型和NSString*类型实质上没有什么区别,给出了具体类型,实质为了更好的找出语法方面的bug.在运行时发现消息都会去寻找消息的执行。
绝对不要用id*,指针的指针是没有意义的。
例子代码:
@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
eg:这里必须用类方法
if([obj isKindOfClass :[NSString class])
selector是用来描述方法的,不用再写参数的类型和名字,写上调用的关键字和冒号
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的方法-(id)copy; -(id)mutableCopy;、 -(NSString*)description ,用在NSLog,%@。
NSString对象
NSString 对Unicode编码的任意语言的字符串,可以容纳任何语言。用@"" 编译成NSString。
NSString是不可变的,会返回一个新的字符串。
NSString的使用方法太多,建议查看文档使用。
NSString已经优化的性能非常的好了,最好不要使用MutableString,可变版本,使用append也新建一个字串过程和MutableString一样。
NSNumber 封装原始数据比如 Int float double等。
NSValue 封装非对象的数据,为结构体之类的数据提供包装,这些数值,CGPoint
NSData 用来装无结构数据,二进制,传递他们的Api,图片等
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; //保留顺序输出为数组,放弃顺序变成NSSet
NSMutableOrderSet
- (void)insertObject:(id)anObject atIndex:(int)anIndex; - (void)removeObject:(id)anObject; - (void)setObject:(id)anObject atIndex:(int)anIndex;
Enumeration//枚举
NSArray *myArray=...; for(NSString *string in myArray){ double value=[string doubleValue]; } 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
表示任何下面6个类型的组合NSArray, NSDictionary, NSNumber, NSString, NSDate, NSData
这有这6种都是property List,因为IOS有的API参数是id,文档规定这个id必须是Property List
11、NSUserDefaults是轻量级的property List存储
通过standardUserDefaults方法来存取,以字典为基础,存于应用运行的空隙[NSUserDefaults standardUserDefaults] setArray :rvArray forKey:@"RecentlyViewed"];
常用方法
- (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];//方法来同步到去存储,任何操作后都要存储一下,开销不大。
相关文章推荐
- NSMutableString可变字符串的定义及常用方法
- 关于JSONArray明明添加add JSONObject却为空
- OC基础-零基础学习Objective-C:第四部分.NSArray
- OC基础-零基础学习Objective-C:第五部分.@property
- OC基础-零基础学习Objective-C:第六部分.ARC内存管理
- objective-c 实现常用算法(冒泡、选择、快速、插入)
- Swift Tutorial(swift 教程) 3:Arrays, Objects, and Classes数组,对象和类
- gobject base class
- iOS开发系列—Objective-C之内存管理(前辈写的,借鉴了一下,超赞)
- swift中导入Objective-c的第三方库(swift与oc混编)
- gobject type system
- 将List<Map<String, Object>>排序,升序、降序
- java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter. <init>(Z)V
- iOS利用block实现链式编程方法(Objective-C链式编程)
- OC之ARC环境中的循环strong问题
- iPhone Development 101: Objective-C: Objective-C: Custom URL Schemes
- OC之@class
- OC之set方法内存管理
- [Swift] Any VS AnyObject
- WaitForSingleObject()函数、WaitForMultipleObject()函数