iOS - Swift - 返回异步检索值的函数 [英] iOS - Swift - Function that returns asynchronously retrieved value

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

问题描述

所以我有一个来自 Parse 的 PFFile 对象,我正在尝试创建一个函数来检索该 PFFile 的 UIImage 表示并返回它.类似的东西:

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
}

然而,这里的问题很明显.我每次都会得到 nil,因为 getDataInBackroundWithBlock 函数是异步的.在返回图像变量之前,有没有办法等到 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.

推荐答案

是的,可以这样做.它称为 closure,或更常见的是 callback.callback 本质上是一个函数,您可以将其用作其他函数中的参数.参数的语法是

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 通常是 Void.在你的情况下,你可以使用

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*/}
)

如果你的回调函数转义了主函数(回调在主函数返回后调用),你必须在回调的参数类型前加上@escaping

If your callback function escapes the main function (the callback is called after the main function returns), you must add @escaping in front of the callback's argument type

您将要使用一个回调,该回调将在函数返回后被调用并且包含 UIImage? 作为结果.

You're going to want to use a single callback that will be called after the function returns and that contains UIImage? as the result.

所以,你的代码可能看起来像这样

So, your code could look something like this

func imageFromFile(file: PFFile, result: @escaping (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 - Swift - 返回异步检索值的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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