買了本《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></p><p>{</p><p> NSString*name;</p><p>}</p><p>@property(copy) NSString*name;</p><p>@end</p><p>@implementation A</p><p>@synthesize name;</p><p>-(id)copyWithZone:(NSZone*)zone<br />{</p><p> A *a;<br /> a=[[[self class]allocWithZone:zone]init];<br /> a.name=self.name;<br /> return a;<br />}</p><p>-(void)encodeWithCoder:(NSCoder*)acoder<br />{<br /> [acoder codeWithObject:name forKey:@"name"];<br />}<br />-(id)initWithCoder:(NSCoder*)decoder<br />{<br /> if(self=[super init])<br /> {<br /> NSString*n=[decoder decodeObjectForKey:@"name"];</p><p> }<br /> return self;<br />}<br />
通過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<br />{<br />NSString*name;<br />}<br />@property NSString*name;<br />@end<br />@implementation A<br />@synthesize name;<br />@end<br />@interface B;NSObject<br />{<br />NSMutableArray*a;<br />}<br />@end<br />@implementation B<br />-(void)addA(NSString*n)<br />{<br />A*object;<br />object.name=n;<br />a.add(object);<br />}<br />@end<br />int main()<br />{<br />B* bObject=[[B alloc]init];<br />bObject.addA("A");<br />bObject.addA("B");<br />bObject.addA("C");<br />bObject.addA("D");<br />NSPredicate*predicate=[NSPredicate predicateWithFormat:@"name=%@","A"];<br />NSArray*result=[bObject filteredArrayUsingPredicate: predicate]; </p><p>}
謂詞還給了很多的運算子(可以沿用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*'前面有兩個字元