An analysis of the use of Block syntax in ios development and the use of iosblock

Source: Internet
Author: User
Tags call back

An analysis of the use of Block syntax in ios development and the use of iosblock

In fact, the development of any application is inseparable from multithreading. Block and GCD are the core of multithreading in Apple OC. 1. the internal structure of a block is in oc. The block syntax exists in the form of a closure. Each Objective-C object occupies a certain memory area. The block itself is also an object. In the memory area of the object storing the block, the first variable is a pointer to the Class, which is called isa. The remaining memory contains various information required for the normal operation of the block object. The following are the Internal Structure Variables of block syntax. 1. void * isa (Object Pointer to class) 2.int flags 3.int reserved 4. void (*) (void *,......) Invoke (function pointer) 5. struct * descriptor (struct description) according to the memory structure of the block syntax above, the most important is the invoke variable. This is a function pointer pointing to the block implementation code.
II. Application of blocks in system api callback
In iOS native APIs, block syntax is used in many places. The most common one is the block enumeration of arrays and dictionaries. This enumeration method is provided by each NSArry and NSDictionary, its efficiency and convenience are much higher than the traditional for loop.

[Self. p_tableArrenumerateObjectsUsingBlock: ^ (id obj, NSUInteger idx, BOOL * stop ){

<# Code #>

}]


Another common feature is the complete callback after page Jump to the present,

[Self presentViewController: <(UIViewController *)> animated: <(BOOL)> completion: <^ (void) completion>]


Iii. Centralized code of block syntax in UIAlertView

UIAlertView is a pop-up control with some buttons. The most common is confirmation and cancellation. UIAlertView has a proxy to process these buttons. After a user clicks the button, the proxy can capture the subscript of the button, then you can write some logic code based on the subscript Index.

-(Void) alertView :( UIAlertView *) alertView clickedButtonAtIndex :( NSInteger) buttonIndex


The first use of the quick syntax is that you can write the logic Code directly when creating the UIAlertView control, and do not need to write it in the above callback.

The following are specific implementations.

Step 1: declare a whole Block object. According to the callback proxy of UIAlertView, the main parameter of this proxy is an NSInteger parameter, the Block object we declare can be created based on the actual situation of the proxy.

@ Property (nonatomic, copy) void (^ p_alertBlock) (NSInteger );


The second part is to implement all the Block objects in the place where the UIAltView is created.

If (! Self. p_alertBlock)

{

Self. p_alertBlock = ^ (NSInteger buttonIndex)

{


AccountViewController * acc = accc;

Switch (buttonIndex ){

Case0:

Break;

Case1:

If ([acc. p_fmdbdeleteObjc: list])

{

[Acc. p_tableArrremoveObjectAtIndex: acc. p_didSelect];

[Acc. p_tablereloadData];

}

Break;

}

};

}

UIAlertView * alt = [[UIAlertViewalloc] initWithTitle: NSLocalizedString (@ "tip", nil) message: NSLocalizedString (@ "sureDelet", nil) delegate: selfcancelButtonTitle: NSLocalizedString (@ "cancel", nil) otherButtonTitles: NSLocalizedString (@ "OK", nil), nil];

[Altshow];

This is a pop-up window for deleting databases. When you click OK, you can delete the database objects and click Cancel without any processing. Step 3: Run fast in the proxy of UIAlertView.

-(Void) alertView :( UIAlertView *) alertView clickedButtonAtIndex :( NSInteger) buttonIndex

{

Self. p_alertBlock (buttonIndex );

}


One advantage of this usage is that you can determine the processing of its buttons before the UIAlertView is created. The centralized code looks more convenient and intuitive.

The above is just an example, which can be implemented in many controls in the system, such as UIActionSheet.


3. Block syntax instead of Delegate

The proxy Delegate is indispensable for initiating ios message notifications. Although the proxy is convenient, the implementation process is cumbersome. If the Block syntax is used, we can encapsulate the Code Implementation of the callback function into the Block in advance and pass it as a parameter to the data layer. This is actually in the proxy callback.

If (self. delegate & [self. delegaterespondsToSelector: @ selector (setAddressSuccess :)])

{

[Self. delegatesetAddressSuccess: self. accountList. remark];

}

This sentence will be

Block (self. accountList. remark); is replaced.

Is it more convenient? The key is that you do not need to declare the proxy, implement, set the proxy, maintain the protocol, and so on.

Another restriction on proxy is that it must be objectized. If a tool class is an instance method of a class and you want to call back the data to the C layer after some operations, the proxy cannot be implemented at this time, but Block is not just an object, just like NSString, it can help you implement it.


The above is just the use of block syntax in the system control API callback and proxy between the two classes. More functions need to be found in actual development.

The wise, the Wise, and the extent to which block syntax is powerful, you will know when you use more.


Related Article

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.