IOS开发(58)之在 Block Object 中获取变量
1 前言
通过本次学习,我们就能够理解在 objective-c 方法和在 block objects 中获取变量的区别。
2 代码实例
main.m
[plain]
#import <foundation/foundation.h>
#import "testdemo.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
testdemo *test = [[testdemo alloc] init];
// [test log1];
// [test simplemethod];
// [test simplemethod2];
// [test simplemethod3];
// [test callcorrectblockobject];
// [test simplemethod4];
// [test log2];
// [test scopetest];
[test scopetest2];
}
return 0;
}
#import <foundation/foundation.h>
#import "testdemo.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
testdemo *test = [[testdemo alloc] init];
// [test log1];
// [test simplemethod];
// [test simplemethod2];
// [test simplemethod3];
// [test callcorrectblockobject];
// [test simplemethod4];
// [test log2];
// [test scopetest];
[test scopetest2];
}
return 0;
}
testdemo.h
[plain] view plaincopyprint?#import <foundation/foundation.h>
@interface testdemo : nsobject
@property (nonatomic, strong) nsstring *stringproperty;
-(void)log1;
- (void) simplemethod;
- (void) simplemethod2;
- (void) simplemethod3;
- (void) callcorrectblockobject;
- (void) simplemethod4;
-(void)log2;
- (void) scopetest;
-(void) scopetest2;
@end
#import <foundation/foundation.h>
@interface testdemo : nsobject
@property (nonatomic, strong) nsstring *stringproperty;
-(void)log1;
- (void) simplemethod;
- (void) simplemethod2;
- (void) simplemethod3;
- (void) callcorrectblockobject;
- (void) simplemethod4;
-(void)log2;
- (void) scopetest;
-(void) scopetest2;
@end
testdemo.m
[plain]
#import "testdemo.h"
@implementation testdemo
@synthesize stringproperty;
void (^independentblockobject)(void) = ^(void){
nsinteger localinteger = 10;
nslog(@"local integer = %ld", (long)localinteger);
localinteger = 20;
nslog(@"local integer = %ld", (long)localinteger);
};
-(void)log1{
independentblockobject();
}
//只读
- (void) simplemethod{
nsuinteger outsidevariable = 10;
nsmutablearray *array = [[nsmutablearray alloc]
initwithobjects:@"obj1",
@"obj2", nil];
[array sortusingcomparator:^nscomparisonresult(id obj1, id obj2) {
nsuinteger insidevariable = 20;
nslog(@"outside variable = %lu", (unsigned long)outsidevariable);
nslog(@"inside variable = %lu", (unsigned long)insidevariable);
/* return value for our block object */
return nsorderedsame;
}];
}
//可该
- (void) simplemethod2{
__block nsuinteger outsidevariable = 10;
nsmutablearray *array = [[nsmutablearray alloc]
initwithobjects:@"obj1",
@"obj2", nil];
[array sortusingcomparator:^nscomparisonresult(id obj1, id obj2) {
nsuinteger insidevariable = 20;
outsidevariable = 30;
nslog(@"outside variable = %lu", (unsigned long)outsidevariable);
nslog(@"inside variable = %lu", (unsigned long)insidevariable);
/* return value for our block object */
return nsorderedsame;
}];
}
//获得self
- (void) simplemethod3{
nsmutablearray *array = [[nsmutablearray alloc]
initwithobjects:@"obj1",
@"obj2", nil];
[array sortusingcomparator:^nscomparisonresult(id obj1, id obj2) {
nslog(@"self = %@", self);
/* return value for our block object */ return nsorderedsame;
}]; }
//无变化block object要获得 self需要传参数进去
void (^correctblockobject)(id) = ^(id self){
nslog(@"self = %@", self);
};
- (void) callcorrectblockobject{
correctblockobject(self); }
//修改类属性
- (void) simplemethod4{
nsmutablearray *array = [[nsmutablearray alloc]
initwithobjects:@"obj1",
@"obj2", nil];
[array sortusingcomparator:^nscomparisonresult(id obj1, id obj2) {
nslog(@"self = %@", self);
self.stringproperty = @"block objects";
nslog(@"string property = %@", self.stringproperty);
/* return value for our block object */
return nsorderedsame;
}];
}
//在独立 block object 内部,你不能使用 dot notation 读写一个已声明属性,在这个场景中可以使用这个合成属性的 getter and setter 方法来代替 dot notation:
void (^correctblockobject1)(id) = ^(id self){ nslog(@"self = %@", self);
/* this will work fine */
[self setstringproperty:@"block objects"]; /* this will work fine as well */
nslog(@"self.stringproperty = %@",[self stringproperty]);
};
-(void)log2{
correctblockobject1(self);
}
//当出现在内联 block objects 中,有一条非常重要的规则你必须记住:内联 block objects 在其词法区域 会为这些变量复制值。此处发生的事情是在执行 block 的地方 block object 自身一直有一个 integervalue 变量的只读复制。
typedef void (^blockwithnoparams)(void);
- (void) scopetest{
nsuinteger integervalue = 10;
/*************** definition of internal block object ***************/
blockwithnoparams myblock = ^{
nslog(@"integer value inside the block = %lu", (unsigned long)integervalue);
};
/*************** end definition of internal block object ***************/ integervalue = 20;
/* call the block here after changing the
value of the integervalue variable */
myblock();
nslog(@"integer value outside the block = %lu",(unsigned long)integervalue);
}
//可改变变量值测试
-(void) scopetest2{
__block nsuinteger integervalue = 10;
/*************** definition of internal block object ***************/
blockwithnoparams myblock = ^{
nslog(@"integer value inside the block = %lu", (unsigned long)integervalue);
};
/*************** end definition of internal block object ***************/ integervalue = 20;
/* call the block here after changing the
value of the integervalue variable */
myblock();
nslog(@"integer value outside the block = %lu",
(unsigned long)integervalue);
}
@end
#import "testdemo.h"
@implementation testdemo
@synthesize stringproperty;
void (^independentblockobject)(void) = ^(void){
nsinteger localinteger = 10;
nslog(@"local integer = %ld", (long)localinteger);
localinteger = 20;
nslog(@"local integer = %ld", (long)localinteger);
};
-(void)log1{
independentblockobject();
}
//只读
- (void) simplemethod{
nsuinteger outsidevariable = 10;
nsmutablearray *array = [[nsmutablearray alloc]
initwithobjects:@"obj1",
@"obj2", nil];
[array sortusingcomparator:^nscomparisonresult(id obj1, id obj2) {
nsuinteger insidevariable = 20;
nslog(@"outside variable = %lu", (unsigned long)outsidevariable);
nslog(@"inside variable = %lu", (unsigned long)insidevariable);
/* return value for our block object */
return nsorderedsame;
}];
}
//可该
- (void) simplemethod2{
__block nsuinteger outsidevariable = 10;
nsmutablearray *array = [[nsmutablearray alloc]
initwithobjects:@"obj1",
@"obj2", nil];
[array sortusingcomparator:^nscomparisonresult(id obj1, id obj2) {
nsuinteger insidevariable = 20;
outsidevariable = 30;
nslog(@"outside variable = %lu", (unsigned long)outsidevariable);
nslog(@"inside variable = %lu", (unsigned long)insidevariable);
/* return value for our block object */
return nsorderedsame;
}];
}
//获得self
- (void) simplemethod3{
nsmutablearray *array = [[nsmutablearray alloc]
initwithobjects:@"obj1",
@"obj2", nil];
[array sortusingcomparator:^nscomparisonresult(id obj1, id obj2) {
nslog(@"self = %@", self);
/* return value for our block object */ return nsorderedsame;
}]; }
//无变化block object要获得 self需要传参数进去
void (^correctblockobject)(id) = ^(id self){
nslog(@"self = %@", self);
};
- (void) callcorrectblockobject{
correctblockobject(self); }
//修改类属性
- (void) simplemethod4{
nsmutablearray *array = [[nsmutablearray alloc]
initwithobjects:@"obj1",
@"obj2", nil];
[array sortusingcomparator:^nscomparisonresult(id obj1, id obj2) {
nslog(@"self = %@", self);
self.stringproperty = @"block objects";
nslog(@"string property = %@", self.stringproperty);
/* return value for our block object */
return nsorderedsame;
}];
}
//在独立 block object 内部,你不能使用 dot notation 读写一个已声明属性,在这个场景中可以使用这个合成属性的 getter and setter 方法来代替 dot notation:
void (^correctblockobject1)(id) = ^(id self){ nslog(@"self = %@", self);
/* this will work fine */
[self setstringproperty:@"block objects"]; /* this will work fine as well */
nslog(@"self.stringproperty = %@",[self stringproperty]);
};
-(void)log2{
correctblockobject1(self);
}
//当出现在内联 block objects 中,有一条非常重要的规则你必须记住:内联 block objects 在其词法区域 会为这些变量复制值。此处发生的事情是在执行 block 的地方 block object 自身一直有一个 integervalue 变量的只读复制。
typedef void (^blockwithnoparams)(void);
- (void) scopetest{
nsuinteger integervalue = 10;
/*************** definition of internal block object ***************/
blockwithnoparams myblock = ^{
nslog(@"integer value inside the block = %lu", (unsigned long)integervalue);
};
/*************** end definition of internal block object ***************/ integervalue = 20;
/* call the block here after changing the
value of the integervalue variable */
myblock();
nslog(@"integer value outside the block = %lu",(unsigned long)integervalue);
}
//可改变变量值测试
-(void) scopetest2{
__block nsuinteger integervalue = 10;
/*************** definition of internal block object ***************/
blockwithnoparams myblock = ^{
nslog(@"integer value inside the block = %lu", (unsigned long)integervalue);
};
/*************** end definition of internal block object ***************/ integervalue = 20;
/* call the block here after changing the
value of the integervalue variable */
myblock();
nslog(@"integer value outside the block = %lu",
(unsigned long)integervalue);
}
@end
运行结果
2013-05-09 22:25:34.087 blockobjectparamtest[946:303] integer value inside the block = 20
2013-05-09 22:25:34.089 blockobjectparamtest[946:303] integer value outside the block = 20
上一篇: 图片水印添加专家使用教程