itenyh
itenyh

Reputation: 1939

Can not change argument value in block

I came across a problem when using block. Look at the simple example I created :

enter image description here

Say , I have a global variable, and I want to change its value by passing it as an argument of the function "requstWithArg:". But the value is changed in a block. So, an error happened , because I can not declare the argument to be a __block type.

How to solve this problem? Thanks!

Upvotes: 0

Views: 39

Answers (1)

Brad Allred
Brad Allred

Reputation: 7534

well type id is simply a pointer to an object, so why cant you declare a new variable above the block declaration like this: __block id myVar = arg;

so you would have:

__block id myVar = arg;
void (^blk)(void) = ^(void){myVar = [NSString string];};

I cant test this right now, but does it not work for some reason?

However, I'm also a bit confused at what exactly you are tying to do, this looks like a nonsensical design to me. It looks like you are taking some arbitrary object pointer and clobbering it with a new NSString... am I missing something?

Upvotes: 2

Related Questions