in fact, the development of any one application is left multithreaded. "Block" blocks and GCD are the core of multi-threading in the Apple OC language. I. Internal structure of the block in OC, block syntax exists in the form of closures, where each Objective-c object occupies an area of memory. The block itself is also an object, in the memory area of the object that holds the block, the first variable is a pointer to class, which is called Isa. The rest of the memory contains all kinds of information required for the Block object to function properly. The following is the internal structure variable of the block syntax. 1.void* ISA (Pointer to class object)2.int Flags3.int reserved4.void (*) (void*, ...) ) Invoke (function pointer)5.struct* Descriptor (structural description) according to the memory structure of the block syntax above, the most important one is the invoke variable, which is a function pointer, which points to the implementation code of the block.
Two. Application of block in System API callback
In the native API of iOS, there are a lot of places where the block syntax is used. The most common of these is two places, one is the block enumeration of arrays and dictionaries, this enumeration is every nsarry andnsdictionary are all self-bringing, their efficiency and convenience is much higher than the traditional for loop.
[Self. P_tablearr enumerateobjectsusingblock: ^ (id obj,nsuinteger idx, BOOL *stop) {
< #code #>
}]
Another common place is the completion callback after the page jump present,
[ Self presentviewcontroller:< (Uiviewcontroller *) > animated:< (BOOL) > completion:<^ (void) Completion>]
Three. Block syntax in Uialertview in the centralized code
Uialertview is a popup control, there are buttons, the most common is to confirm and cancel, Uialertview has an agent to handle these buttons, when the user clicks, the agent can capture the subscript of the button, The user then writes some logic code based on the subscript index.
-(void) Alertview: (uialertview *) Alertview clickedbuttonatindex: (nsinteger) buttonindex
The first magical thing about fast grammar is that you can write logic code directly when you create the Uialertview control, without writing it in the callback above.
The following is a concrete implementation.
The first step, declare a whole block object, according to Uialertview's callback agent, the principal parameter of the agent is a Nsinteger type parameter, then we declare the block object can be created according to the actual situation of the agent.
@property(nonatomic,copy)void(^p_alertblock) (nsinteger);
In the second part, the entire block object is implemented where this uialtview is created.
if (! Self . P_alertblock)
{
Self. P_alertblock = ^ (nsinteger buttonindex)
{
accountviewcontroller *ACC = ACCC;
Switch (Buttonindex) {
Case 0:
break;
Case 1:
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 database operation to delete the popup window, when the user click on the confirmation to delete the database object, click Cancel do not do any processing. The third step is to run fast in the Uialertview agent.
-(void) Alertview: (uialertview *) Alertview clickedbuttonatindex: (nsinteger) buttonindex
{
Self. P_alertblock (Buttonindex);
}
One advantage of this usage is that it is possible to determine the processing of its buttons before Uialertview has been created, centralizing the code, and looking more convenient and intuitive.
These are just a few examples that can be implemented in many of the system's controls, such as Uiactionsheet.
three. Block syntax substitution delegate
When you bring up your iOS message notification, you'll have agent delegate. Although the agent is very convenient, but the implementation of the process is more cumbersome. If we use block syntax, we can encapsulate the code implementation of the callback function into the block as a parameter to the data layer, which is actually in the proxy callback
if (self.) delegate && [self. Delegaterespondstoselector:@selector(setaddresssuccess:)])
{
[self. Delegatesetaddresssuccess:self. Accountlist. Remark];
}
This sentence has been
Block (self. ) accountlist . remark); Replaced.
Is it more convenient? The key is that you don't have to declare the agent, implement, set up the agent, maintain the protocol and so on complex process.
The agent also has a restriction is to have to object, if a tool class is an instance of a class method, you want to call back the data to the C layer after some operations, this time the agent can not be implemented, but the block is not an object only, like NSString, to help you achieve.
The above is just the magic of block syntax in the system control API callback and the magic of the agent between the two classes, more features still need to be gradually excavated in the actual development of discovery.
The benevolent see, the Block grammar is really strong to what extent, and so you use more to know.
A brief analysis of the magical application of block syntax in IOS development