iOS版 - 斯威夫特 - 函数返回异步检索值 [英] iOS - Swift - Function that returns asynchronously retrieved value

查看:112
本文介绍了iOS版 - 斯威夫特 - 函数返回异步检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个PFFile对象从parse,我试图创建一个检索PFFile的UIImage的重presentation并返回的功能。是这样的:

So I have a PFFile object from Parse, and I'm trying to create a function that retrieves the UIImage representation of that PFFile and returns it. Something like:

func imageFromFile(file: PFFile) -> UIImage? {
    var image: UIImage?

    file.getDataInBackgroundWithBlock() { (data: NSData?, error: NSError?) -> Void in
        if error != nil {
            image = UIImage(data: data!)
        }
    }

    return image
}

然而,这里的问题是显而易见的。我要得到零每次都因为getDataInBackroundWithBlock功能是异步的。有什么办法等待,直到返回image变量之前的UIImage已检索?我不知道,如果使用同步的getData()是在这种情况下,去的有效方法。

However, the problem here is obvious. I'm going to get nil every time because the getDataInBackroundWithBlock function is asynchronous. Is there any way to wait until the UIImage has been retrieved before the image variable is returned? I don't know if using the synchronous getData() is an efficient way to go in this case.

推荐答案

是的,这是可以做到这一点。它被称为<一href=\"https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html#//apple_ref/doc/uid/TP40014097-CH11-ID94\"><$c$c>closure,更常见的一个回调。 A 回调基本上可以作为另一个函数的参数使用的功能。争论的语法为

Yes, it is possible to do this. Its called a closure, or more commonly a callback. A callback is essentially a function that you can use as an argument in another functions. The syntax of the argument is

functionName: (arg0, arg1, arg2, ...) -> ReturnType

的返回类型通常是虚空。你的情况,你可以使用

ReturnType is usually Void. In your case, you could use

result: (image: UIImage?) -> Void

在它调用一个函数与一个回调的语法

The syntax of calling a function with one callback in it is

function(arg0, arg1, arg2, ...){(callbackArguments) -> CallbackReturnType in
    //code
}

和调用几个回调函数的语法(缩进,使其更易于阅读)

And the syntax of calling a function with several callbacks is (indented to make it easier to read)

function(
    arg0, 
    arg1,
    arg2,
    {(cb1Args) -> CB1Return in /*code*/},
    {(cb2Args) -> CB2Return in /*code*/},
    {(cb3Args) -> CB3Return in /*code*/}
)

您会想使用一个回调包含的UIImage?作为一个结果。所以,你的code可能看起来像这样

You're going to want to use a single callback contains a UIImage? as a result. So, your code could look something like this

func imageFromFile(file: PFFile, result: (image: UIImage?) -> Void){
    var image: UIImage?

    file.getDataInBackgroundWithBlock() { (data: NSData?, error: NSError?) -> Void in
        //this should be 'error == nil' instead of 'error != nil'. We want
        //to make sure that there is no error (error == nil) before creating 
        //the image
        if error == nil {
            image = UIImage(data: data!)
            result(image: image)
        }
        else{
            //callback nil so the app does not pause infinitely if 
            //the error != nil
            result(image: nil)
        }
    }
}

和调用它,你可以简单地使用

And to call it, you could simply use

imageFromFile(myPFFile){(image: UIImage?) -> Void in
    //use the image that was just retrieved
}

这篇关于iOS版 - 斯威夫特 - 函数返回异步检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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