从异步块斯威夫特,返回值 [英] Swift, return value from asynchronous block

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

问题描述

很新的iOS开发和我刚开始在GCD涉足,我在所以读几篇文章,并曾咨询苹果公司的文档,但是我努力理解如何从昂贵的操作设置一个返回值

Very new to iOS development and I've just started dabbling in GCD, I've read several articles on SO and have consulted Apple's documentation, however I am struggling to understand how to set a return value from an "expensive" operation.

下面的函数将遍历总和100K用户(作为一个例子),在主线程上做这显然会停止几秒钟的图形用户界面,因此在我的UserList类,我建议我的线程同步执行此块

The below function will loop over sum 100K users (as an example), doing this on the main thread would obviously halt the GUI for several seconds, therefore in my UserList class, I propose to execute this block asynchronously on my its thread:

let queue = dispatch_queue_create("me.alexsims.user_queue", DISPATCH_QUEUE_CONCURRENT)

public func getUserById(userId: String, completionHandler: (result: User) -> ())
{
    dispatch_async(queue)
    {
        for user in self.users {
            if user.getUserId() == userId
            {
                completionHandler(result: user)
            }
        }
        // Return an empty user object
        completionHandler(result: User())
    }
}

现在从我的理解,结果变量应返回,那么我应该能够访问用户()存储在那里从我的主线程对象

Now from my understanding, the result variable should be returned and I should then be able to access the User() object which is stored in there from my main thread

然而,当我回到我的控制器,并运行一个测试:

However, when I return to my controller and run a test:

var list   = UserList()
var a_user = User()

a_user = list.getUserByID(userId: "xyz", completionHandler: { (result) -> () in
    println(result)
})

这引发错误,因为我不是在主队列找不到'调用println接受提供的参数,我猜这就是超载?所以我尝试:

this throws the error Could not find an overload for 'println' that accepts the supplied arguments, I'm guessing thats because I am not on the main queue? So I try:

a_user = list.getUserById(userId: "xyz", completionHandler: { (result) -> () in
    dispatch_async(dispatch_get_main_queue()) {
        println(result)
    }
})

不过,我仍然得到同样的错误。我在做什么错在这里?

However, I still get the same error. What am I doing wrong here?

解决
丹尼尔发现,问题提供了可选的参数传递给 list.getUserByID 电话。
将其更改为后:

SOLVED As Daniel spotted, the problem was providing the optional parameter to the list.getUserByID call. After changing it to:

a_user = list.getUserById("abcde", completionHandler: { (result) -> () in
    println(result)
})

该错误的println消失。

The println error vanished.

推荐答案

您雨燕类用户需要实现可打印的协议,它可以通过的println使用。

Your Swift class User needs to implement the Printable protocol so that it can be used by println.

事情是这样的:

Class User: Printable {
  let name = "Bob"
  var description: String {
    return "User: \(name)"
  }
}

阅读本更多<一个href=\"https://developer.apple.com/library/ios/documentation/General/Reference/SwiftStandardLibraryReference/Printable.html\"相对=nofollow>苹果后

如果你感觉懒惰,使您的用户类的NSObject的子类已经实现了可打印协议

If you are feeling lazy, make your User class a subclass of NSObject which already implements the Printable protocol

一个快速搜索你的println错误的转向<一个href=\"http://stackoverflow.com/questions/24223970/could-not-find-an-overload-for-println-that-accepts-the-supplied-arguments\">this达。不知道为什么错误落空给println但继承人修复:

A quick search of your println error turned this up. Not sure why the error falls through to println but heres a fix:

尝试删除您的函数调用的第一个参数,斯威夫特则不需要输入的第一个参数,以避免冗余:

Try removing the first parameter of your function call, in Swift you don't need to type the first parameter to avoid redundancy:

a_user = list.getUserById(用户名:某某...

应该是:

a_user = list.getUserById(XYZ ...

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

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