斯威夫特确实有异步编程语言是什么级别的支持(如果有的话)? [英] What language level support (if any) does Swift have for asynchronous programming?

查看:101
本文介绍了斯威夫特确实有异步编程语言是什么级别的支持(如果有的话)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

异步编程是响应的用户界面的必备应用程序时,有超过未predictable网络(例如智能手机的应用程序)进行通信。在等待结果从服务器回来的地方在互联网上的用户界面必须保持响应。

Asynchronous programming is a must for responsive user interfaces when application have to communicate over unpredictable networks (e.g. smart phone applications). The user interface must remain responsive while waiting for results to come back from servers somewhere over the internet.

在大多数语言中,应用程序员必须实现自己的状态机(也许使用闭包),以响应异步回调和/或使用锁协调乘法线程。

In most languages, the application programmer has to implement their own state machines (maybe using closures) to respond to asynchronous callbacks and/or coordinate multiply threads using locks.

这两者都是非常容易出错,并且不为既成事实心地!

Both of these are very error prone and not for the fait hearted!

(C#引入了异步关键字来解决这个问题,只是时间(至少5年)会告诉我们,如果它是一个很好的解决方案

(c# introduced the async keyword to help with this, only time (at least 5 years) will tell if it is a good solution.)

雨燕是否有任何内置的支持,以协助异步code的写作?

Does Swift have any built in support to assist the writing of asynchronous code?

推荐答案

斯威夫特的方法异步编程是一样的目标C的:使用中央调度。你可以通过闭包gcd上述DISPATCH-功能,就像在ObjC。然而,对于美观的原因,你也可以在关闭括号后通过你的闭合(块):

Swift's approach to asynchronous programming is the same as Objective C's: use Grand Central Dispatch. You can pass closures to gcd dispatch_ functions, just as in ObjC. However, for aesthetic reasons, you can also pass your closure (block) after the close parentheses:

dispatch_async(dispatch_get_main_queue()) {
    println("async hello world")
}

这篇关于斯威夫特确实有异步编程语言是什么级别的支持(如果有的话)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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