Simple use of block

Source: Internet
Author: User

Block is a useful post-great learning. Now I'm just going to list a few basic uses.

1. Fast Enumeration (enumeration)

It is usually used with Nsarray, Nsdictionary, Nsset, and Nsindexset.

When combined with these two things, the block usually has two uses. (Code for instance operation)

I. The first block usage is an enumeration, and some operations are performed on each enumerated object, and the block return value is void

II. The index of the second enumeration object, which, of course, is returned after some tests.

    

The first usage returns a value of 0 and operates on each object 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];
}];
 
The second usage, returned is a indexnsarray *array by passtest = [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 Multithreading

Here is a direct example. Suppose we have a uilable, now nsjsonserialization to parse a JSON file for an address. Now the implementation is to use this uilable to indicate the loading state, such as the Text property before loading it should be @ "is loading", after loading is @ "have loaded" specific look at the code

In the Viewcontroller of the uilable viewdidappear Statuslable.text = @ "is loading";d ispatch_queue_t QQ = Dispatch_queue_ Create ("Com.sayalittle", nil);//Even if you use arc, remember to write a Dealloc method to release the queue, but there is no need to use the [Super Dealloc]dispatch_async ( Queue, ^{               nserror *error;           Suppose a local array is used to receive the JSON parsed array            self.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);
}//because the UI has always been done within the main thread, it is now updated in the main thread after parsing the data.

As I said before, a dealloc to release the queue.



The basic usage of the list of foreign friends

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 I TEMs in the array instead of the items. The range of items to enumerate is passed via the Indexset parameter. Here indexes can be used Nsmakerange (0, 3) This from me to create and + (ID) Indexsetwithindexesinrange: (nsrange)Indexrange
    • Indexesofobjectspassingtest:–the Block returns an indexset of the the the objects so 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 this 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 C Ode when the animation code has completed.

Grand Central Dispatch

    • Dispatch_async–this is the main function for async GCD code.

Simple use of block

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.