1、函数返回指针或数组:
必须将局部变量定义为静态(static )变量。
int * getRandom( ){ static int r[10]; int i; /* set the seed */ srand( (unsigned)time( NULL ) ); for ( i = 0; i < 10; ++i) { r[i] = rand(); NSLog(@"%d", r[i] ); } return r;}
2、块
声明:
returntype (^blockName)(argumentType);
实现:
returntype (^blockName)(argumentType)= ^{
};
void (^simpleBlock)(void) = ^{
NSLog(@"This is a block");
};
double (^multiplyTwoValues)(double, double) =
^(double firstValue, double secondValue) {
return firstValue * secondValue;
};
double result = multiplyTwoValues(2,4);
NSLog(@"The result is %f", result);
3、使用类型定义块
#import <Foundation/Foundation.h>
typedef void (^CompletionBlock)();
@interface SampleClass:NSObject
- (void)performActionWithCompletion:(CompletionBlock)completionBlock;
@end
@implementation SampleClass
- (void)performActionWithCompletion:(CompletionBlock)completionBlock{
NSLog(@"Action Performed");
completionBlock();
}
@end
int main()
{
/* my first program in Objective-C */
SampleClass *sampleClass = [[SampleClass alloc]init];
[sampleClass performActionWithCompletion:^{
NSLog(@"Completion is called to intimate action is performed.");
}];
return 0;
}