Block Object call for IOS development (59)

Source: Internet
Author: User

1 Preface
This article describes how to call Block Object by using functions and how to call Block Object by using Block Object.

2. code example
TestDemo. h


[Plain]
# Import <Foundation/Foundation. h>
 
@ Interface TestDemo: NSObject
 
-(Void) callSimpleBlock;
-(Void) callTrimBlock;
@ End

# Import <Foundation/Foundation. h>

@ Interface TestDemo: NSObject

-(Void) callSimpleBlock;
-(Void) callTrimBlock;
@ End
TestDemo. m

 

[Plain]
# Import "TestDemo. h"
 
@ Implementation TestDemo
 
 
/************** Call the Block Object Start ***************/
Void (^ simpleBlock) (NSString *) = ^ (NSString * paramString ){
/* Implement the block object here and use the paramString parameter */
NSLog (@ "% @", paramString );
};
-(Void) callSimpleBlock {
SimpleBlock (@ "Archy ");
}
/************** Call the Block Object End ***************/
 
/************** Block Object call Block Object Start ***************/
NSString * (^ trimString) (NSString *) = ^ (NSString * inputString ){
NSString * result = [inputString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet];
Return result;
};

NSString * (^ trimWithOtherBlock) (NSString *) = ^ (NSString * inputString ){
Return trimString (inputString );
};
-(Void) callTrimBlock {
NSString * trimmedString = trimWithOtherBlock (@ "Archy ");
NSLog (@ "Trimmed string = % @", trimmedString );
}
/************** Block Object call Block Object Start ***************/
 
@ End

# Import "TestDemo. h"

@ Implementation TestDemo


/************** Call the Block Object Start ***************/
Void (^ simpleBlock) (NSString *) = ^ (NSString * paramString ){
/* Implement the block object here and use the paramString parameter */
NSLog (@ "% @", paramString );
};
-(Void) callSimpleBlock {
SimpleBlock (@ "Archy ");
}
/************** Call the Block Object End ***************/

/************** Block Object call Block Object Start ***************/
NSString * (^ trimString) (NSString *) = ^ (NSString * inputString ){
NSString * result = [inputString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet];
Return result;
};

NSString * (^ trimWithOtherBlock) (NSString *) = ^ (NSString * inputString ){
Return trimString (inputString );
};
-(Void) callTrimBlock {
NSString * trimmedString = trimWithOtherBlock (@ "Archy ");
NSLog (@ "Trimmed string = % @", trimmedString );
}
/************** Block Object call Block Object Start ***************/

@ End
Main. m

 

[Plain] view plaincopyprint? Int main (int argc, const char * argv [])
{
 
@ Autoreleasepool {

TestDemo * test = [[TestDemo alloc] init];
// [Test callSimpleBlock];
[Test callTrimBlock];
}
Return 0;
}

Int main (int argc, const char * argv [])
{

@ Autoreleasepool {

TestDemo * test = [[TestDemo alloc] init];
// [Test callSimpleBlock];
[Test callTrimBlock];
}
Return 0;
}
Running result


06:53:50. 893 CallBlockObjectTest [591: 303] Trimmed string = Archy

 

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.