如何在块中更改int值? [英] How to change int value in block?

查看:102
本文介绍了如何在块中更改int值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改块中的int值,我有这个:

How change int value in block, I have this :

__block long long size = -1;
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{

    ALAssetRepresentation * rep = [myasset defaultRepresentation];
    size = [rep size];
    //here showed normal value
    NSLog(@"needed size : %lld",size);
};
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetslibrary assetForURL:self.tmpVideoURL 
               resultBlock:resultblock
              failureBlock:nil];

//but here remaind -1
NSLog(@"out block value : %lld",size);

推荐答案

问题是您要在assetForURL:...方法完成其工作后,将该块发送出去,以便稍后执行,这是在做异步.它最有可能在后台线程或队列上,从而允许该方法本身在工作继续进行时立即返回.

The problem is that you're sending that block off to be executed sometime later, after the assetForURL:... method has done its work, which it's doing asynchronously. It's most likely on a background thread or queue, allowing the method itself to return immediately while the work continues.

因此,方法assetForURL:...resultBlock运行之前返回 ,这意味着到第二个NSLog时,该值尚未更改.一切正常.您只是过早检查该值.

So the method assetForURL:... returns before your resultBlock has run, meaning the value hasn't been changed yet, by the time you get to the second NSLog. Everything's working fine; you're just checking the value too early.

这篇关于如何在块中更改int值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆