Apple 的新编程语言 Swift 如何处理块和异步请求? [英] How does Apple's new programming language Swift handle blocks and asynchronous requests?

查看:27
本文介绍了Apple 的新编程语言 Swift 如何处理块和异步请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C.f.Apple 的 Swift 网站页面:https://developer.apple.com/swift/

C.f. Apple's website page on Swift: https://developer.apple.com/swift/

Swift 中是否有像 Objective-c 中那样的块?它们是如何创建和调用的?

Are there blocks in Swift like in objective-c? How are they created and called?

如何在 Swift 中执行异步请求?

How would do an asynchronous request in Swift?

是否容易在 swift 中创建与块相关的内存泄漏?如果是,您将如何避免它们?

Is it easy to create block related memory leaks in swift? If yes, how would you avoid them?

推荐答案

(Objective-)C 块的 Swift 等价物称为闭包.有 aThe Swift Programming Language 一书中关于它们的整章.

The Swift equivalent of an (Objective-)C block is called a closure. There's a whole chapter about them in The Swift Programming Language book.

根据您使用闭包的上下文,您可以使用非常简洁的语法声明/使用它.例如,采用签名为 (success: Bool, error: NSError) 的完成处理程序的方法 - >Void 可以这样调用:

Depending on the context where you use a closure, you can declare/use it with very concise syntax. For example, a method that takes a completion handler whose signature is (success: Bool, error: NSError) - > Void can be called like this:

someMethod(otherParameters: otherValues, completionHandler:{ success, error in
    if !success { NSLog("I am a leaf on the wind: %@", error) }
})

还有一个尾随闭包语法,在闭包本质上提供流控制的情况下读起来很好.当您想要非常简短时,您可以删除参数名称(以降低可读性为代价,但在某些明显的情况下(如下所示)也可以).return 语句通常也是隐式的.

There's also a trailing closure syntax that reads nicely in cases where a closure essentially provides flow control. And you can drop the parameter names when you want to be really brief (at some cost to readability, but that's okay in some obvious cases like the below). Often a return statement is implicit, too.

myArray.sort { $0 < $1 }
let squares = myArray.map { value in
    value * 2
}    

Swift 本身没有用于异步请求的任何东西,因此您可以使用现有的 API.不过,您可以使用尾随闭包语法:

Swift itself doesn't have anything for asynchronous requests, so you use existing API for that. You can use the trailing closure syntax, though:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
    // do some async stuff
    NSOperationQueue.mainQueue().addOperationWithBlock {
        // do some main thread stuff stuff
    }
}

在大多数情况下,您无需像使用 ObjC 块那样担心使用 Swift 闭包创建引用循环.简而言之,捕获语义足够相似,可以按照您希望的方式对大多数东西工作",但又足够不同,以至于块/闭包使用的通用模式(例如分派到后台/主线程和引用 self 的属性)不会导致循环.

In most cases, you don't need to worry about creating reference cycles with Swift closures the way you do with ObjC blocks. To put it simply, the capture semantics are similar enough to "just work" the way you want it to for most stuff, but different enough that the common patterns for block/closure uses (e.g. dispatch to background/main thread and referencing self's properties) don't cause cycles.

不过,循环仍然是可能的,并且有一个解决方案.这个答案已经有点长了,所以看看 用于闭包的强参考循环 完整解释在文档中.

Cycles are still possible, though, and there is a solution for them. This answer's a bit long already, so check out Strong Reference Cycles for Closures in the docs for the complete explanation.

这篇关于Apple 的新编程语言 Swift 如何处理块和异步请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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