lovejuly
lovejuly

Reputation: 11

Build failure, cannot refer to a declaration with a variably modified type inside block error in iOS?

This is my code which cause the error mentioned above, I have seen a similar question asked by others, but the only answer do not fully address my question, because I doubt that this question should has some better answers.

CGPoint pointArray[lines.count];

[lines enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    NSLog(@"%f", pointArray[idx].y);
}];

Upvotes: 1

Views: 928

Answers (1)

rmaddy
rmaddy

Reputation: 318774

As indicated by Cannot refer to declaration with a variably modified type inside block and its linked duplicate How to write into an array from a dispatch_apply (GCD) loop?, you can't access a C-array inside an Objective-C block.

One solution would be to store your CGPoint objects in an NSArray using NSValue.

NSArray *pointArray = @[
    [NSValue valueWithCGPoint:CGPointMake(0, 0)],
    [NSValue valueWithCGPoint:CGPointMake(10, 20)]
];

Once you have your pointArray with your CGPoint instances, then your block becomes:

[lines enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    NSLog(@"%f", pointArray[idx].CGPointValue.y);
}];

Upvotes: 1

Related Questions