包含闭包的swift函数的空返回值

编程入门 行业动态 更新时间:2024-10-23 11:28:48
本文介绍了包含闭包的swift函数的空返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我创建了一个函数,该函数应该返回一个字典,其中填充了在线检索的数据(使用json,基于Ray Wenderlich tut)。该代码处于封闭状态。问题是首先返回一个空字典,然后才填充它。不知道这是否与获取远程数据有些延迟有关,但显然我需要在返回之前先填充字典。这是代码。

I created a function that should return a dictionary filled with data that are retrieved (using json, based on Ray Wenderlich tut) online. That code is in a closure. The problem is that an empty dictionary is returned first, and only afterwards it gets filled. Don't know if this is related to some delay in getting the remote data, but obviously I need the dictionary to be filled first before returning it. Here is the code.

func getStatusFromRemoteSource() -> [StatusModel] { var statusUpdates = [StatusModel]() println("statusUpdates after initialization: \(statusUpdates)") // 1 DataManager.getStatusDataWithSuccess { (statusData) -> Void in let json = JSON(data: statusData) if let jsonArray = json.array { for jsonItem in jsonArray { var statusVersion: String? = jsonItem["version"].string var statusDescription: String? = jsonItem["message"].string var statusCode: Int? = jsonItem["code"].string!.toInt() var update = StatusModel(version: statusVersion, message: statusDescription, code: statusCode) statusUpdates.append(update) println("statusUpdates after appending update: \(statusUpdates)") // 3 (after other function call) } let item = 0 println("Version \(statusUpdates[item].version) has status \(statusUpdates[item].message)") // println("Status code: \(statusUpdates[item].code)") } } println("Status updates before return: \(statusUpdates)") // 2 return statusUpdates }

所以 // 1 首先打印,然后 // 2 (仍为空)然后调用另一个函数(调用此函数)。只有那时 // 3 打印(正确)与应检索的内容。

So //1 prints first, then //2 (still empty) and then the other function (that calls this one) is called. Only then //3 is printed (correctly) with the content that should be retrieved.

我如何填写返回之前 statusUpdates 字典?

How can I fill the statusUpdates dictionary before returning it?

推荐答案

你应该使用Closures in返回statusUpdates作为其Async方法的方法。 空状态更新将立即在您的代码中返回,但在使用闭包时,您可以等到DataManager.getStatusDataWithSuccess完成:

You should use Closures in method to return statusUpdates as its Async method. The empty statusUpdates will be returned immediately in your code but when using closures, you can wait till DataManager.getStatusDataWithSuccess is finished:

typealias RemoteStatusHandler = (status:[StatusModel]) -> Void func getStatusFromRemoteSource(handler:RemoteStatusHandler){ var statusUpdates = [StatusModel]() println("statusUpdates after initialization: \(statusUpdates)") // 1 DataManager.getStatusDataWithSuccess { (statusData) -> Void in let json = JSON(data: statusData) if let jsonArray = json.array { for jsonItem in jsonArray { var statusVersion: String? = jsonItem["version"].string var statusDescription: String? = jsonItem["message"].string var statusCode: Int? = jsonItem["code"].string!.toInt() var update = StatusModel(version: statusVersion, message: statusDescription, code: statusCode) statusUpdates.append(update) println("statusUpdates after appending update: \(statusUpdates)") // 3 (after other function call) } let item = 0 println("Version \(statusUpdates[item].version) has status \(statusUpdates[item].message)") // println("Status code: \(statusUpdates[item].code)") } handler(status: statusUpdates) } }

然后可以像这样调用你的函数:

Then your function can be called like this:

getStatusFromRemoteSource { (status) -> Void in //use status here, this function is void. }

更多推荐

包含闭包的swift函数的空返回值

本文发布于:2023-11-26 18:28:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634690.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   返回值   swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!