ios - Assign new object to parameter object in block -


say have block so:

object someobject = nil;  block = ^(object *obj){      if(obj == nil)         obj = [[object alloc] init]; };  block(someobject); //someobject still nil block(someobject); //it assign again, instead of not  nslog(@"result: %@", someobject); //still nil 

it seems though cant assign things parameters in blocks way, there way it? block acts on different objects, , needs assign if object nil. in state, object assigned in scope (so obj assigned, not someobject). ive tried using __block dont think thats for.

when comes parameters, blocks act functions. doing here similar how nserrors being handled apple's api.

try this:

object * someobject = nil;  block = ^(object **obj){      if(obj != nil && *obj == nil)         *obj = [[object alloc] init]; };  block(&someobject); //someobject still nil block(&someobject); //it assign again, instead of not  nslog(@"result: %@", someobject); 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -