Objective-c Grammar Quick over (8)

Source: Internet
Author: User


block (OC data type, very common, essentially C struct)


Similar to inline functions, from the source code layer, there is the structure of the function, but after compiling, but does not have the nature of the function. At compile time, similar to macro substitution, use function body to replace function name at call



Block encapsulates a piece of code that can be executed at any time



A block can be used as a function parameter or as a return value for a function, and it can have either an input parameter or a return value.



Apple's official proposal to use block as much as possible. In multi-threaded, asynchronous tasks, collection traversal, collection sorting, animation transitions with a lot of



Blocks definition of:


 
 
int (^MySum)(int, int) = ^(int a, int b) {

     return a+b;

};


Defines a blocks object called MySum, which has two int parameters and returns an int. The right side of the equation is the concrete implementation of blocks, where the block can access local variables, but cannot be modified.



Block resembles a statement, such as 10;


int sum = 10;
int (^ MyBlock) (int) = ^ (int num) {
     sum ++; // Compile error
     return num * sum;
}; 


Add keyword if you want to modify: __block



__block int sum = 10;



can access global variables and can also modify global variables



Blocks comparison with function pointers (similar)



Defining function pointers



Int (*MYFN) ();



Define Blocks



Int (^myblocks) (int,int);



Calling function pointers



(*MYFN) (10, 20);



Call blocks



Myblocks (10, 20);



Blocks the assigned value



Define the variable at the same time as the declaration, then assign the value


 
 
int (^MySum)(int,int) = ^(int a,int b) {

     return a + b;

};


You can also first declare the type with a TypeDef, and then define the variable to assign the value


 
typedef int (^MySum)(int,int);

MySum sum = ^(int a,int b) {

     return a + b;

}; 


What the block needs to know.
1> How to define a block variable (two types)


int (^sumblock) (intint); void (^myblock) ();


2> How to use block encapsulation code


^(int a, int b) {
return a - b;
};

^() {
NSLog(@"----------");
};


3> block access outside variables



* External variables can be accessed inside the block
* By default, the outside local variables cannot be modified inside the block
* Add __block keyword to local variable, this local variable can be modified inside block

4> defining block types with typedef


typedef int (^ MyBlock) (int, int);
// Later, you can use MyBlock to define the block variable
MyBlock block;
MyBlock b1, b2;

b1 = ^ (int a, int b) {
return a-b;
};

MyBlock b3 = ^ (int a, int b) {
return a-b;
}; 
protocol Simple to use (very common)Basic Use


Used to declare a whole bunch of methods (not declaring member variables), just declarations!



As long as a class adheres to this protocol, it is equivalent to having all the method declarations in this agreement



As long as the parent class adheres to a protocol, the subclass also adheres to the



The preparation of the agreement


@protocol Agreement name // method Declaration List @end


A class adheres to the agreement


@interface class Name: Parent class < Agreement name >@end


There are 2 keywords in the protocol that control whether the method is to be implemented (by default, @required), and in most cases the use of communication between programmers


    • @required: This method must be implemented (if not implemented, the compiler warns)
    • @optional: This method is not necessarily implemented


Agreement Compliance Agreement


    • One protocol can comply with multiple protocols, and multiple protocols are separated by commas.
    • A protocol that complies with other agreements is equivalent to having a method declaration in another agreement
@protocol Agreement name < Protocol 1, protocol 2>@end
Base Protocol
    • NSObject is a base class, the most fundamental and basic class, and any other class will eventually inherit it.
    • In fact, there is a protocol, the name is also called NSObject, it is a base protocol, the most fundamental and basic protocol
    • Many of the most basic methods are stated in the NSObject protocol, such as description, retain, release, etc.
    • It is recommended that each new agreement be subject to the NSObject agreement.
specify protocol when defining variables
// NSObject type objects, and must comply with the NSCopying protocol

NSObject <NSCopying> * obj;

// Any OC object, and must comply with the NSCoding protocol

id <NSCoding> obj2; 


How to create a new protocol in XCODE6: OC file is similar to the following window (including the new category)






Create the. h file because the protocol is used to declare the method. declaration in the header file.


// MyProtocol.h
#import <Foundation / Foundation.h>
// Define a protocol named MyProtocol
@protocol MyProtocol <NSObject>
// @required requires implementation, warns if not implemented
// @optional does not require implementation
-(void) test4;
@required
-(void) test;
-(void) test2;
@optional
-(void) test3;
@end

// MyProtocol2.h
#import <Foundation / Foundation.h>
@protocol MyProtocol2 <NSObject>
-(void) haha2;
@optional
-(void) haha3;
@end

// MyPrototol3.h
#import <Foundation / Foundation.h>
#import "MyProtocol.h"
// One protocol adheres to another protocol, and can have all method declarations of another protocol
@protocol MyProtocol3 <MyProtocol>
-(void) hehe;
@end

/ * File name: Person.h * /
#import <Foundation / Foundation.h>
@class Hashiqi;
@protocol MyProtocol2;
@protocol MyProtocol3;
// As long as a class adheres to a certain agreement, it can have all method declarations in that agreement
//: inheritance
// <> obey the agreement
@interface Person: NSObject <MyProtocol3, MyProtocol2>
@property (nonatomic, strong) id <MyProtocol2> obj;
@property (nonatomic, strong) Hashiqi * dog;
@end

/ * File name: Person.m * /
#import "Person.h"
#import "MyProtocol2.h"
#import "MyProtocol3.h"

@implementation Person
-(void) test
{}
@end

// MyProtocol4.h
#import <Foundation / Foundation.h>
@protocol MyProtocol4 <NSObject>
@end

/ * File name: Dog.h * /
#import <Foundation / Foundation.h>

@protocol MyProtocol2;
@interface Dog: NSObject <MyProtocol2>
@end

/ * File name: Dog.m * /
#import "Dog.h"
#import "MyProtocol2.h"

@implementation Dog
-(void) haha2
{
}
@end

/ * File name: Hashiqi.h * /
#import "Dog.h"

@protocol MyDogProtocol <NSObject>
-(void) dogTest;
@end

@interface Hashiqi: Dog <MyDogProtocol>
-(void) addTest;
@end

@interface Hashiqi (Add)
-(void) addTest;
@end

/ * File name: Hashiqi.m * /
#import "Hashiqi.h"

@implementation Hashiqi

-(void) dogTest
{

}

@end

@implementation Hashiqi (Add)
-(void) addTest
{

}
@end

// Hashiqi + MJ.h
#import "Hashiqi.h"

@interface Hashiqi (MJ)
@end

// Hashiqi + MJ.m
#import "Hashiqi + MJ.h"
@implementation Hashiqi (MJ)
@end

// main.m
#import <Foundation / Foundation.h>
#import "MyProtocol.h"
#import "MyProtocol3.h"
#import "Person.h"
#import "Dog.h"
#import "Hashiqi.h"

int main ()
{
    Person * p = [[Person alloc] init];
    p.obj = [[Hashiqi alloc] init];
    
    return 0;
}

void test ()
{
    // NSObject * obj = [[NSObject alloc] init];
    
    
    // NSObject * obj2 = @ "4324324";
    
    
    // Requires that the object saved by obj3 must comply with the protocol of MyProtocol
    
    // NSObject <MyProtocol> * obj3 = [[NSObject alloc] init];
    
    NSObject <MyProtocol> * obj3 = [[Person alloc] init];
    
    obj3 = nil;
    
    // id is equivalent to NSObject *
    id <MyProtocol> obj4 = [[Person alloc] init];
    
    obj4 = nil;
    
    // requires obj5, the saved object must conform to MyProtocol3, and inherit Person
    Person <MyProtocol3> * obj5 = [[Person alloc] init];
    
    obj5 = nil;
} 



@protocol protocol name <NSOBJECT>
//method declaration list ....
@end


1> Class Compliance Protocol
@interface Class name: Parent class name < protocol name 1, protocol name 2& Gt
@end

2> Protocol Compliance Agreement
@protocol Agreement name < Other protocol Name 1, other agreement name 2>
@end

3. The keyword of the method declaration in the agreement
1> @req Uired (default)
requires implementation, and if not implemented, warns

2> @optional
does not require implementation, there will be no warning

4. When defining a variable, limit the object to which the variable is saved to adhere to a protocol
class name < agreement name > * variable name;
id< protocol name > variable name;
Nsobject<myprotocol> *obj;
Id<myprotocol> Obj2;

If the corresponding protocol is not followed, the compiler warns that properties declared in

[email protected] can also be used as a compliance restriction
@property (nonatomic, Strong) class name < agreement name > * attribute name;
@property (nonatomic, strong) id< Protocol name > property name;



@property (nonatomic, strong) dog<myprotocol> *dog;
@property (nonatomic, strong) id<myprotocol> dog2;

6. The agreement can be defined in a separate. h file and can also be defined in a class
1> If this protocol is used only in a class, the protocol should be defined in that class

2> If this protocol is used in many classes, it should be defined in a separate file

7. Classifications can be defined in separate. h and. m files and can also be defined in the original class
1> in general, are defined in a separate file
2> defines the classification in the original class, only requires the ability to read the grammar



Objective-c Grammar Quick over (8)


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.