關於寒假學習objective-c的感想

來源:互聯網
上載者:User

買了本《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*'前面有兩個字元

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.