Objective C 只读 int?什么?
我做了一个整数来计算我的一个流程有多少成功。在我的代码之外,我声明:
int successes = 0
。
然后在我的循环中,我有 successes++;
,此时 XCode 抱怨“变量不可分配(缺少 _block 类型说明符)”。
到底是怎么回事?为什么我不能增加我的 int 值?我从未声明它是只读的...
非常感谢任何帮助。
我使用的代码是:
_block int successes = 0;
for(CLLocation *location in locationOutputArray)
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error)
{
if(placemarks && placemarks.count > 0)
{
successes++;
CLPlacemark *topResult = [placemarks objectAtIndex:0];
NSString *address = [NSString stringWithFormat:@"%@ %@,%@ %@", [topResult subThoroughfare],[topResult thoroughfare],[topResult locality], [topResult administrativeArea]];
[addressOutputArray addObject:address];
NSLog(@"%@",address);
}
}];
[geocoder release];
}
I made an int to count how many successes one of my processes has. Outside my code, I declare:
int successes = 0
.
Then within my loop, I have successes++;
, at which point XCode complains that "variable is not assignable (missing _block type specifier)".
What is going on? Why can't I increment my int? I never declared it read-only...
Any help is much appreciated.
The code I used is:
_block int successes = 0;
for(CLLocation *location in locationOutputArray)
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error)
{
if(placemarks && placemarks.count > 0)
{
successes++;
CLPlacemark *topResult = [placemarks objectAtIndex:0];
NSString *address = [NSString stringWithFormat:@"%@ %@,%@ %@", [topResult subThoroughfare],[topResult thoroughfare],[topResult locality], [topResult administrativeArea]];
[addressOutputArray addObject:address];
NSLog(@"%@",address);
}
}];
[geocoder release];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的循环位于块内(
^{...}
语法)。如果变量没有__block
说明符,则块无法更改块外部的变量。Your loop is inside a block (
^{...}
syntax). Blocks cannot alter variables outside of the block without that variable having a__block
specifier.您尝试在块内访问此
int
。将其标记为__block
,以便可以从块内更新它。块编程主题
You tried accessing this
int
inside a block. Mark it as__block
so it can be updated from within the block.Blocks Programming Topics