买了本《learn objective-c on Mac》,一开始买来看起来还蛮薄的,250页,看了后,发现语言都有很多共同的地方,新的特性都从C++和java类似过来,就是换了个名字,感觉看了以后感觉收获蛮大,虽然只接触了点皮毛。
1.C语言知识
2.了解了xcode使用
3.懂得了一些新语法和新特性。
---------------------------------------------------------------------------------------------
1.正式协议 与 非正式协议(被@optional的正式协议代替) 与文件加载保存
@required 一定要实现
@optional 选择性实现
例1.
@protocol NSCopying //深度复制复制
-(id) copyWithZone:(NSZone*)zone;
@end
NSArray的copy为浅复制
@protocol NSCoding //编码与解码
-(void)encodeWithCoder:(NSCoder*)acoder;
-(id)initWithCoder:(NSCoder*)decoder;
NSCoding可用于将任何对象编码为NSData NSData可用 [data writeToFile:"tmp.txt" atomically:true] 存入硬盘
例1:
@interface A:NSObject<NSCopying,NSCoding> { NSString*name; } @property(copy) NSString*name; @end @implementation A @synthesize name; -(id)copyWithZone:(NSZone*)zone { A *a; a=[[[self class]allocWithZone:zone]init]; a.name=self.name; return a; } -(void)encodeWithCoder:(NSCoder*)acoder { [acoder codeWithObject:name forKey:@"name"]; } -(id)initWithCoder:(NSCoder*)decoder { if(self=[super init]) { NSString*n=[decoder decodeObjectForKey:@"name"]; } return self; }
通过NSKeyedArchiver类进行编码为NSData
NSData*data=[NSKeyedArchiver archivedDataWithRootObject:对象];自动调用encodeWithCoder
A *a=[NSKeyedUnarchiver unarchiveObjectWithData:data]; //解码
------------------------------------------------------------------------------------------------------------
属性列表 NSArray NSDictionary NSString NSNumber NSDate NSData
1.NSArray
+(id)arrayWithObject:... 以nil结尾
-(unsigned)count;
-(id)objectAtIndex:(unsigned) index;
NSMutableArray
+(id)arrayWithCapacity:(int);
-(void)addObject:(NSObject);
-(void)removeObjectAtIndex:(int)index;
2.NSDictionary
+(id)dicitonaryWithObjectsAndKeys:(id)firstObject,(NSString*)firstkey...;
-(id)objectForKey:(id)key;
NSMutableDictionary
+(id)dicitonaryWithCapacity:(int)n;
-(void)setObject:(id) object forKey:(id)key;
-(void)removeObjectForKey:(id)key;
3.NSNumber
封装标准类型
NSNumber*a=[NSNumber numberWithInt:10];
4.NSDate
+(NSDate*)date;
+(NSDate*)dateWithTimeIntervalSinceNow:(int)a;
5.NSData
+(NSData*)dataWithBytes:(NSObject) length:(int);
-(int)length;
-(NSObject) bytes;
--------------------------------------------------------------------------------------------------------
谓词:
谓词实际上就是用简单的方式给出限制条件 可以用for 和if来替代
NSPredicate:
+(NSPredicate)predicateWithFormat:....
-(NSArray*)filteredArrayUsingPredicate:predicate;
例:@interface A :NSObject { NSString*name; } @property NSString*name; @end @implementation A @synthesize name; @end @interface B;NSObject { NSMutableArray*a; } @end @implementation B -(void)addA(NSString*n) { A*object; object.name=n; a.add(object); } @end int main() { B* bObject=[[B alloc]init]; bObject.addA("A"); bObject.addA("B"); bObject.addA("C"); bObject.addA("D"); NSPredicate*predicate=[NSPredicate predicateWithFormat:@"name=%@","A"]; NSArray*result=[bObject filteredArrayUsingPredicate: predicate]; }
谓词还给了很多的运算符(可以沿用C的运算符)
例:沿用上了例子的类
NSPredicate*predicate=[NSPredicate predicateWithFormat:@"(name=='A')AND(name=='B')"];
数组运算符 name BETWEEN{MIN,MAX};
name IN{"A","B"};
SELF的运用:
NSArray*a=[NSArray arrayWithObject:@"A","B",nil];
NSPredicate*predicate=[NSPredicate predicateWithFormat:@SELF IN " 'A','B' "];
NSArray* b=[a filteredArrayUsingPredicate:predicate];
字符串运算符:
BEGINSWITH
ENDSWITH
CONTAINS
[c]忽略大小写
[d]忽略重音符
[cd]都忽略
例: BEGINSWITH[c]
LIKE运算符
LIKE '*A*' 中间包含A即可
LIKE '??A*'前面有两个字符