i want create dynamic class has property block. want make block function able access class instance variables , properties.
here code doubt:
/* myclass interface */ @interface myclass:nsobject @property (nonatomic, strong) nsstring *variable; @property (nonatomic, assign) void (^updatefunction)(); @end /* myclass implementation */ @implementation myclass -(void)update{ //perform block function code self.updatefunction(); } @end /* myclass usage*/ myclass *myclass = [[myclass alloc]init]; myclass.variable = @"variable value"; myclass.updatefunction = ^{ //here doubt... //how can access myclass.variable , sure when block //will called didn't bad access? } so previous code doubt is: how access instance variables object directly block has been stored property of object itself.
your block function should have argument instance:
@property (nonatomic, assign) void (^updatefunction)(myclass *obj); then call block:
self.updatefunction(self); so can you:
myclass.variable = @"variable value"; myclass.updatefunction = ^(myclass *obj) { nslog(@"%@", obj.variable); }; [myclass update];
Comments
Post a Comment