1.Block里的引用類(lèi)型不做賦值操作,是可以不用在變量前加__block的。
ex:
NSMutableArray *getData = [NSMutableArray array];
__block int i = 0;
dispatch_async(queeDoSth, ^{
//此處不要做=號(hào)操作。貌似就不需要強(qiáng)制使用__block;
[getData addObject:[NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com.cn"] encoding:NSUTF8StringEncoding error:nil]];
//此處若要改變i的值聲明的時(shí)候必須用__block來(lái)修飾。
i = 1;
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"i had get www.google.com.cn");
});
});
2.block的使用場(chǎng)合,據(jù)說(shuō)最好不要過(guò)多的使用,在需要代碼段作為參數(shù),and如上的gcd操作。
ex:
NSLog(@"2*2+4=%d",^(int a){return a*a;}(2)+4);
ps:^(int a){return a*a;}(2)這個(gè)就是整體作為一個(gè)表達(dá)式,來(lái)寫(xiě)。
也可以分開(kāi)來(lái)試試寫(xiě)
int(^getDouble)(int) = ^(int a){return a*a;};
NSLog(@"2*2+4=%d",getDouble(2)+4);
這例子就是引出怎么用,具體深層的,要google鳥(niǎo)。這東西的生命期出了{(lán)}就結(jié)束了(如果里面有什么變量需要留住可能就需要retain下),所以慎用,小道消息說(shuō)這東西還容易內(nèi)存溢出。
3.gcd多線程的使用,這東西據(jù)說(shuō)是c的效率高,有人說(shuō)寫(xiě)法很漂亮(粗人就不發(fā)表意見(jiàn)了)。明顯的好處避免使用開(kāi)關(guān)鎖來(lái)付出額外的開(kāi)銷(xiāo),最主要的是看起來(lái)很高端(這個(gè)隨便說(shuō)說(shuō))。
ex:
1>.同步線程
NSMutableArray *getData = [NSMutableArray array];
//創(chuàng)建一個(gè)隊(duì)列。
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");
//因?yàn)槭峭降?,所以?xiě)在最后一個(gè)。
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");
});
});
//這樣疊加。如果需要在線程跑完后干什么,就還得加個(gè)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,這東西很神奇,有點(diǎn)sql的味道,可以來(lái)驗(yàn)正則,判斷范圍,和in操作,第一次見(jiàn)就喜歡上了。
ex:
1>.簡(jiǎn)單的正則匹配。
NSString *regexp = @"^\\d+$";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self matches %@",regexp];
NSLog(@"all number:%@",[predicate evaluateWithObject:@"123"] ?@"Y" :@"N");
2>.還有它的拿手戲,過(guò)濾數(shù)組。
NSString *regexp = @"^\\d+$";
NSArray *array = [NSArray arrayWithObjects:@"x",@"1123",@"3456",@"yy", nil];
NSLog(@"all number item:%@",[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self matches %@",regexp]]);
3>.對(duì)數(shù)組里的元素進(jìn)行過(guò)濾
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]]);
//換個(gè)動(dòng)詞。
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:這東西很靈活,用起來(lái)也很沒(méi)有邊境,巴實(shí)噢。
5.斷言,判斷表達(dá)式是否是YES,如果是就會(huì)中斷,不能繼續(xù)往下執(zhí)行,且只是在debug下才會(huì)終止程序運(yùn)行。
NSAssert(condition,desc),他衍生出很多版本,NSAssert1,NSAssert2,NSAssert3,NSAssert4,NSAssert5后面數(shù)字是幾就可以多帶幾個(gè)arguments。
NSAssert1(condition,@"wrong:%@",@"xxx");
NSAssert2(condition,@"wrong1:%@,wrong2:%@",@"xxx",@"yyy");
6.調(diào)試,斷點(diǎn)。這里我只引出下,在debug模式下在console命令模式中的運(yùn)用,p就是print打印一個(gè)值,po就是print object打印一個(gè)對(duì)象,n就是next往下執(zhí)行,c就是continue繼續(xù)執(zhí)行正在調(diào)試的程序。該命令用在程序 由于處理信號(hào)或斷點(diǎn)而導(dǎo)致停止運(yùn)行 時(shí)。
7.category,對(duì)已有class擴(kuò)展,有點(diǎn)近似js里的原型擴(kuò)展(prototype)。
ex:
語(yǔ)法就是在類(lèi)后面加個(gè)括號(hào)里面描述下擴(kuò)展的關(guān)鍵字,必須的。
@interface NSString(log)
-(void)stringLog;
@end
@implementation NSString(lossg)
-(void)stringLog{
NSLog(@"log:%@",self);
}
@end
這樣所有的nsstring都擁有了這個(gè)方法,so
NSString *string = @"ddas";
[string stringLog];
8.1>NSCoding這個(gè)協(xié)議,就在序列化的時(shí)候用。
//archivedDataWithRootObject
-(void)encodeWithCoder:(NSCoder *)aCoder{
//可以通過(guò)這個(gè)來(lái)傳值到反序列化的時(shí)候用
[aCoder encodeObject:@"xxxx" forKey:@"ssss"];
}
//unarchiveObjectWithData
-(id)initWithCoder:(NSCoder *)aDecoder{
//這個(gè)就來(lái)取剛才埋下的種子。
self.p = [aDecoder decodeObjectForKey:@"ssss"];
return self;
}
8.2>NSCopying這個(gè)協(xié)議,在對(duì)象copy的時(shí)候用([id copy])。
-(id)copyWithZone:(NSZone *)zone;
如果是淺拷貝
-(id)copyWithZone:(NSZone *)zone{
return self;
}
直接這樣,他們指向同一個(gè)地址。
深拷貝就得改改
-(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);
獻(xiàn)上栗子一枚。
ps,8.1,8.2如果非原生的對(duì)象必須實(shí)現(xiàn)對(duì)應(yīng)的協(xié)議才能用這些序列化,或者copy。不然得死。
補(bǔ)充點(diǎn)深淺拷貝的細(xì)節(jié):
深淺拷貝只針對(duì)指針型的變量類(lèi)型(int,float,bool除外),對(duì)于不可變?nèi)萜鳎礇](méi)有mutable)copy就是淺拷貝,指針指向同一個(gè)地址,mutableCopy就是深拷貝,指針指向不同的地址。
PS:copy后的對(duì)象不能直接新增,刪除元素,而mutableCopy的則可以