浅用block 转
block是一門有用的大后期學(xué)問。現(xiàn)在我只是列出一點(diǎn)基本用法。
?
1.快速枚舉(Enumeration)
通常是和NSArray, NSDictionary, NSSet, NSIndexSet放在一起用。
當(dāng)和以上這兩種東西放在一起用時(shí),通常block有兩種用處。(代碼為實(shí)例操作)
i. 第一種block用法是枚舉后,對每個(gè)枚舉對象進(jìn)行一些操作,block返回值為void
ii. 第二種枚舉對象的index,當(dāng)然這些枚舉對象是通過某些測試后才返回的。
// 第一種用法 返回值為0,對每一個(gè)對象進(jìn)行相應(yīng)操作 NSArray *array = [NSArray arrayWithObjects:@"one", @"two", @"three", nil]; NSMutableArray *mArray = [NSMutableArray alloc]init]; [array enumerateObjectsWithOptions:NSEnumerationConcurrent | NSEnumerationRevese usingBlock:^(id obj, NSUInteger idx, BOOL *stop){[mArray addObject:obj];}];// 第二種用法,返回的是一個(gè)通過passTest的index NSArray *array = [NSArray arrayWithObjects:@"one", @"two", @"three", nil]; NSInteger * index = [array indexOfObjectWithOptions:NSEnumerationConcurrent passingTest: (BOOL)^(id obj, NSUInteger idx, BOOL *stop){NSString *string = (NSString *)obj;return [string hasPrefix:@"O"]; }];2.GCD 多線程
這里就直接舉一個(gè)例子了。假設(shè)我們有一個(gè)UILable,現(xiàn)在NSJSONSerialization 來解析某個(gè)某個(gè)地址的json file。現(xiàn)在要實(shí)現(xiàn)的是用這個(gè)UILable來表示載入狀態(tài),如載入前它的text屬性應(yīng)該是@"is loading", 載入后是@"has loaded" 具體的看代碼
//在該UILable的viewController的ViewDidAppear 中 statusLable.text = @"is loading";dispatch_queue_t qq = dispatch_queue_create("com.sayALittle', nil);// 即使你使用了ARC,也記得需要自己寫一個(gè)dealloc方法來釋放queue,但是這里面就不需要用[super dealloc] dispatch_async(queue, ^{NSError *error;//假設(shè)本地有個(gè)array用來接收J(rèn)SON解析出來的Arrayself.array = [NSJONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:someURL options:kNilOptions error:&error];dispatch_async(dispatch_get_main_queue(), ^{statusLable.text = @"has loaded";}); });- (void)dealloc {dispatch_release(queue); } //因?yàn)閁I的事情一直都是在主線程內(nèi)完成的,所以這里就在解析數(shù)據(jù)后,馬上在主線程中更新了。//如前面說的,要一個(gè)dealloc來釋放queue國外友人的羅列的基本用法
NSArray
- enumerateObjectsUsingBlock?– Probably the Block method I use the most, it basically is a simpler, cleaner foreach.
 - enumerateObjectsAtIndexes:usingBlock:?– Same as enumerateObjectsUsingBlock: except you can enumerate a specific range of items in the array instead of all the items. The range of items to enumerate is passed via the indexSet parameter. // 這里indexes可以用NSMakeRange(0, 3)這種來自我創(chuàng)建 以及+ (id)indexSetWithIndexesInRange:(NSRange)indexRange
 - indexesOfObjectsPassingTest:?– The Block returns an indexset of the the objects that pass a test specified by the Block. Useful for looking for a particular group of objects.
 
NSDictionary
- enumerateKeysAndObjectsUsingBlock:?– Enumerates through a dictionary, passing the Block each key and object.
 - keysOfEntriesPassingTest:?– Returns a set of the keys corresponding to objects that pass a test specified by the Block.
 
UIView
- animateWithDuration:animations:?– UIViewAnimation Block, useful for simple animations.
 - animateWithDuration:completion:?– Another UIViewAnimation Block, this version adds a second Block parameter for callback code when the animation code has completed.
 
Grand Central Dispatch
- dispatch_async?– This is the main function for async GCD code.
 
轉(zhuǎn) :?http://www.cnblogs.com/davidxie/archive/2012/08/23/2652214.html
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/ygm900/p/3176298.html
總結(jié)
                            
                        - 上一篇: 删除链表中指定节点,要求时间复杂度为O(
 - 下一篇: 对 带头结点的单链表 的操作