oc心得

1.Block里的引用類型不做賦值操作,是可以不用在變量前加__block的。

ex:

NSMutableArray *getData = [NSMutableArray array];

__block int i = 0;

dispatch_async(queeDoSth, ^{

//此處不要做=號操作。貌似就不需要強制使用__block;

[getData addObject:[NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com.cn"] encoding:NSUTF8StringEncoding error:nil]];

//此處若要改變i的值聲明的時候必須用__block來修飾。

i = 1;

dispatch_async(dispatch_get_main_queue(), ^{

NSLog(@"i had get www.google.com.cn");

});

});

2.block的使用場合,據說最好不要過多的使用,在需要代碼段作為參數,and如上的gcd操作。

ex:

NSLog(@"2*2+4=%d",^(int a){return a*a;}(2)+4);

ps:^(int a){return a*a;}(2)這個就是整體作為一個表達式,來寫。

也可以分開來試試寫

int(^getDouble)(int) = ^(int a){return a*a;};

NSLog(@"2*2+4=%d",getDouble(2)+4);

這例子就是引出怎么用,具體深層的,要google鳥。這東西的生命期出了{}就結束了(如果里面有什么變量需要留住可能就需要retain下),所以慎用,小道消息說這東西還容易內存溢出。

3.gcd多線程的使用,這東西據說是c的效率高,有人說寫法很漂亮(粗人就不發表意見了)。明顯的好處避免使用開關鎖來付出額外的開銷,最主要的是看起來很高端(這個隨便說說)。

ex:

1>.同步線程

NSMutableArray *getData = [NSMutableArray array];

//創建一個隊列。

dispatch_queue_t queeDoSth = dispatch_queue_create([@"toDoSthBack" UTF8String], NULL);

//添加線程1

dispatch_async(queeDoSth, ^{

[getData addObject:[NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com.cn"] encoding:NSUTF8StringEncoding error:nil]];

dispatch_async(dispatch_get_main_queue(), ^{

NSLog(@"i had get www.google.com.cn");

});

});

//添加線程2

dispatch_async(queeDoSth, ^{

[getData addObject:[NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.jike.com"] encoding:NSUTF8StringEncoding error:nil]];

dispatch_async(dispatch_get_main_queue(), ^{

NSLog(@"i had get www.baidu.com");

//因為是同步的,所以寫在最后一個。

NSLog(@"getData:%@",getData);

});

});

dispatch_release(queeDoSth);

2>.異步線程

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{

NSLog(@"xx");

dispatch_async(dispatch_get_main_queue(), ^{

NSLog(@"yy");

});

});

//這樣疊加。如果需要在線程跑完后干什么,就還得加個group的概念。

dispatch_group_t sysgroup = dispatch_group_create();

dispatch_group_async(sysgroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

[getData addObject:[NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.baidu.com"] encoding:NSUTF8StringEncoding error:nil]];

dispatch_async(dispatch_get_main_queue(), ^{

NSLog(@"i had get www.baidu.com");

});

});

dispatch_group_async(sysgroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

[getData addObject:[NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.jike.com"] encoding:NSUTF8StringEncoding error:nil]];

dispatch_async(dispatch_get_main_queue(), ^{

NSLog(@"i had get www.jike.com");

});

});

dispatch_group_async(sysgroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

[getData addObject:[NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.sina.com"] encoding:NSUTF8StringEncoding error:nil]];

dispatch_async(dispatch_get_main_queue(), ^{

NSLog(@"i had get www.gooojj.com");

});

});

//所有線程完了后干什么事,自己看著辦了。

dispatch_group_notify(sysgroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

NSLog(@"getData:%@",getData);

dispatch_async(dispatch_get_main_queue(), ^{

NSLog(@"all finshed");

});

});

dispatch_release(sysgroup);

4.謂詞,NSPredicate,這東西很神奇,有點sql的味道,可以來驗正則,判斷范圍,和in操作,第一次見就喜歡上了。

ex:

1>.簡單的正則匹配。

NSString *regexp = @"^\\d+$";

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self matches %@",regexp];

NSLog(@"all number:%@",[predicate evaluateWithObject:@"123"] ?@"Y" :@"N");

2>.還有它的拿手戲,過濾數組。

NSString *regexp = @"^\\d+$";

NSArray *array = [NSArray arrayWithObjects:@"x",@"1123",@"3456",@"yy", nil];

NSLog(@"all number item:%@",[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self matches %@",regexp]]);

3>.對數組里的元素進行過濾

NSArray *array = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"x",@"y", nil],[NSDictionary dictionaryWithObjectsAndKeys:@"1x",@"y", nil],[NSDictionary dictionaryWithObjectsAndKeys:@"777",@"y", nil],[NSDictionary dictionaryWithObjectsAndKeys:@"33",@"y", nil],[NSDictionary dictionaryWithObjectsAndKeys:@"x",@"1y", nil], nil];

NSLog(@"y is number:%@",[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self.y matches %@",regexp]]);

//換個動詞。

NSString *regexp = @"x";

NSArray *array = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"x",@"y", nil],[NSDictionary dictionaryWithObjectsAndKeys:@"1x",@"y", nil],[NSDictionary dictionaryWithObjectsAndKeys:@"777",@"y", nil],[NSDictionary dictionaryWithObjectsAndKeys:@"33",@"y", nil],[NSDictionary dictionaryWithObjectsAndKeys:@"x",@"1y", nil], nil];

NSLog(@"the key y is contains x:%@",[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self.y contains %@",regexp]]);

btw:這東西很靈活,用起來也很沒有邊境,巴實噢。

5.斷言,判斷表達式是否是YES,如果是就會中斷,不能繼續往下執行,且只是在debug下才會終止程序運行。

NSAssert(condition,desc),他衍生出很多版本,NSAssert1,NSAssert2,NSAssert3,NSAssert4,NSAssert5后面數字是幾就可以多帶幾個arguments。

NSAssert1(condition,@"wrong:%@",@"xxx");

NSAssert2(condition,@"wrong1:%@,wrong2:%@",@"xxx",@"yyy");

6.調試,斷點。這里我只引出下,在debug模式下在console命令模式中的運用,p就是print打印一個值,po就是print object打印一個對象,n就是next往下執行,c就是continue繼續執行正在調試的程序。該命令用在程序 由于處理信號或斷點而導致停止運行 時。

7.category,對已有class擴展,有點近似js里的原型擴展(prototype)。

ex:

語法就是在類后面加個括號里面描述下擴展的關鍵字,必須的。

@interface NSString(log)

-(void)stringLog;

@end

@implementation NSString(lossg)

-(void)stringLog{

NSLog(@"log:%@",self);

}

@end

這樣所有的nsstring都擁有了這個方法,so

NSString *string = @"ddas";

[string stringLog];

8.1>NSCoding這個協議,就在序列化的時候用。

//archivedDataWithRootObject

-(void)encodeWithCoder:(NSCoder *)aCoder{

//可以通過這個來傳值到反序列化的時候用

[aCoder encodeObject:@"xxxx" forKey:@"ssss"];

}

//unarchiveObjectWithData

-(id)initWithCoder:(NSCoder *)aDecoder{

//這個就來取剛才埋下的種子。

self.p = [aDecoder decodeObjectForKey:@"ssss"];

return self;

}

8.2>NSCopying這個協議,在對象copy的時候用([id copy])。

-(id)copyWithZone:(NSZone *)zone;

如果是淺拷貝

-(id)copyWithZone:(NSZone *)zone{

return self;

}

直接這樣,他們指向同一個地址。

深拷貝就得改改

-(id)copyWithZone:(NSZone *)zone{

LikeCate *tempObject = [[LikeCate alloc] init];

return tempObject;

}

LikeCate *categ = [[LikeCate alloc] init];

categ.p = @"kkkk";

LikeCate *catef ?= [categ copy];

catef.p = @"pppsss";

NSLog(@"pp:%@",catef.p);

categ.p = @"ssss";

NSLog(@"ppp:%@",catef.p);

獻上栗子一枚。

ps,8.1,8.2如果非原生的對象必須實現對應的協議才能用這些序列化,或者copy。不然得死。

補充點深淺拷貝的細節:

深淺拷貝只針對指針型的變量類型(int,float,bool除外),對于不可變容器(即沒有mutable)copy就是淺拷貝,指針指向同一個地址,mutableCopy就是深拷貝,指針指向不同的地址。

PS:copy后的對象不能直接新增,刪除元素,而mutableCopy的則可以

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容