日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

浅用block 转

發(fā)布時間:2024/7/19 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 浅用block 转 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

block是一門有用的大后期學(xué)問。現(xiàn)在我只是列出一點基本用法。

?

1.快速枚舉(Enumeration)

  通常是和NSArray, NSDictionary, NSSet, NSIndexSet放在一起用。

  當(dāng)和以上這兩種東西放在一起用時,通常block有兩種用處。(代碼為實例操作)

    i. 第一種block用法是枚舉后,對每個枚舉對象進行一些操作,block返回值為void

    ii. 第二種枚舉對象的index,當(dāng)然這些枚舉對象是通過某些測試后才返回的。

// 第一種用法 返回值為0,對每一個對象進行相應(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];}];// 第二種用法,返回的是一個通過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 多線程

  這里就直接舉一個例子了。假設(shè)我們有一個UILable,現(xiàn)在NSJSONSerialization 來解析某個某個地址的json file。現(xiàn)在要實現(xiàn)的是用這個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,也記得需要自己寫一個dealloc方法來釋放queue,但是這里面就不需要用[super dealloc] dispatch_async(queue, ^{NSError *error;//假設(shè)本地有個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); } //因為UI的事情一直都是在主線程內(nèi)完成的,所以這里就在解析數(shù)據(jù)后,馬上在主線程中更新了。//如前面說的,要一個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é)

以上是生活随笔為你收集整理的浅用block 转的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。