Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 536 Vote(s) - 3.35 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assign a variable inside a Block to a variable outside a Block

#1
I'm getting an error

>Variable is not assignable (missing __block type specifier)

on the line `aPerson = participant;`. How can I make sure the block can access the `aPerson` variable and the `aPerson` variable can be returned?

Person *aPerson = nil;

[participants enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
Person *participant = (Person*)obj;

if ([participant.gender isEqualToString:@"M"]) {
aPerson = participant;
*stop = YES;
}
}];

return aPerson;
Reply

#2
To assign a variable inside block which outside of block always use __block specifier before that variable your code should be like this:-

__block Person *aPerson = nil;

Reply

#3
Just a reminder of a mistake I made myself too, the

__block

declaration must be done when first declaring the variable, that is, OUTSIDE of the block, not inside of it. This should resolve problems mentioned in the comments about the variable not retaining its value outside of the block.
Reply

#4
Try `__weak` if you get any warning regarding retain cycle else use `__block`

Person *strongPerson = [Person new];
__weak Person *weakPerson = person;

Now you can refer **`weakPerson`** object inside block.
Reply

#5
Just use the `__block` prefix to declare and assign any type of variable inside a block.

For example:

__block Person *aPerson = nil;

__block NSString *name = nil;
Reply

#6
__block Person *aPerson = nil;
Reply

#7
yes block are the most used functionality , so in order to avoid the retain cycle we should avoid using the strong variable,including self inside the block, inspite use the _weak or weakself.
Reply

#8
You need to use this line of code to resolve your problem:

__block Person *aPerson = nil;

For more details, please refer to this tutorial: [Blocks and Variables][1]


[1]:

[To see links please register here]

Reply

#9
When I saw the same error, I tried to resolve it like:

```
__block CGFloat docHeight = 0.0;


[self evaluateJavaScript:@"document.height" completionHandler:^(id height, NSError *error) {
//height
NSLog(@"=========>document.height:@%@",height);
docHeight = [height floatValue];
}];
```
and its working fine

Just add "**__block**" before Variable.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through