Swift中Block的语法

时间:2014-06-04 13:25:33

标签: ios swift swift3

我正在尝试从Objective-C重写为Swift,我无法弄清楚语法或理解文档

以下是Objective-C中的一个简化示例:我写道:

[UIView animateWithDuration:10.0 animations:^{self.navigationController.toolbar.frame = CGRectMake(0,10,0,10);}];

我如何在Swift中写这个?

这是模板自动完成功能:

UIView.animateWithDuration(duration: NSTimeInterval, animations: (() -> Void))

5 个答案:

答案 0 :(得分:16)

这是快速关闭格式:

{(parameter:type, parameter: type, ...) -> returntype in
    //do stuff  
}

这是你应该做的:

//The animation closure will take no parameters and return void (nothing).
UIView.animateWithDuration(duration: NSTimeInterval, animations: {() -> Void in
    //Animate anything.
})

这是闭包的documentation

答案 1 :(得分:10)

由于预期的参数类型和动画参数的返回类型是已知的,编译器可以毫无问题地推断它们。这应该有用(虽然我现在还没有操场可用:

UIView.animateWithDuration(10.0, animations: {
  self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
})

有关闭包的更多信息,请参阅chapter in the swift docs

关于CGRect()的注意事项 - 在快速代码中使用developer docs show CGRect()。也许它需要导入?

更新评论:你也可以使用如下的尾随闭包:

UIView.animateWithDuration(10.0) {
  self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
}

答案 2 :(得分:5)

以下代码可以指导您编写自己的块。

class func testFunc(completion: ((list : NSArray!) -> Void)?) {
    //---  block code.
    if completion! != nil {
        completion! (list: NSArray())
    }
}

您可以将其称为 -

className.testFunc {
(list: NSArray!) -> Void in
}

答案 3 :(得分:3)

你基本上可以用3种相同的方式写它:

在闭包/代码块中写下正确的操作:

UIView.animateWithDuration(10.0) {
  self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
}

这也称为尾随闭包(如果闭包参数是 last 参数,则只能执行尾随闭包)

这并不代表参数动画'不再写了。它是写的,但就像上面的格式一样。

完全写在行内,大多数开发人员都避免这样做,因为用所有的括号和括号写一个小小的错误。

UIView.animateWithDuration(10.0, animations: {
  self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
})

(与尾随封闭相反,你写了名字即动画') 这称为内联关闭

以更模块化的意义书写

UIView.animateWithDuration(duration: NSTimeInterval, animations: animatingFunc)

func animatingFunc() {
  self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
}

请记住参数'动画的类型'是() -> Void

正如我们正在做的那样,animatingFunc没有参数,即'()'并且什么都不返回,即' void'

(在Swift中,函数是类型,可以作为参数传入) 有些人可能会说这更具可读性,有人可能会说尾随封闭是......

旁注 1 你也可以什么都不做(这真的没有意义,但在许多其他处理程序/动画/完成处理程序中你可能不想做任何事情)

UIView.animateWithDuration(duration: NSTimeInterval, animations: nil)

旁注 2

当您必须捕获值时,闭包变得更有趣。请参阅this简单演示。 有关Swift闭包的更多信息,请参阅Apple's Documentation

答案 4 :(得分:2)

如何在Swift中声明关闭?

  1. 作为变量:

    var closureName: (ParameterTypes) -> ReturnType

  2. 作为可选变量:

    var closureName: ((ParameterTypes) -> ReturnType)?

  3. 作为类型别名:

    typealias ClosureType = (ParameterTypes) -> ReturnType

  4. 作为常量:

    let closureName: ClosureType = { ... }

  5. 作为另一个函数的参数:

    funcName(parameter: (ParameterTypes) -> ReturnType)

注意:如果传入的闭包超出了方法的范围,例如如果要将其保存到属性,则需要使用@escaping进行注释。

  1. 作为函数调用的参数:

    funcName({ (ParameterTypes) -> ReturnType in statements })

  2. 作为函数参数:

    array.sorted(by: { (item1: Int, item2: Int) -> Bool in return item1 < item2 })

  3. 作为具有隐式类型的函数参数:

    array.sorted(by: { (item1, item2) -> Bool in return item1 < item2 })

  4. 作为具有隐式返回类型的函数参数:

    array.sorted(by: { (item1, item2) in return item1 < item2 })

  5. 作为最后一个功能参数:

    array.sorted { (item1, item2) in return item1 < item2 }

  6. 使用简写参数名称作为最后一个参数:

    array.sorted { return $0 < $1 }

  7. 作为最后一个参数,带有隐式返回值:

    array.sorted { $0 < $1 }

  8. 作为最后一个参数,作为对现有功能的引用:

    array.sorted(by: <)

  9. 作为具有显式捕获语义的函数参数:

    array.sorted(by: { [unowned self] (item1: Int, item2: Int) -> Bool in return item1 < item2 })

  10. 作为具有显式捕获语义和推断参数/返回类型的函数参数:

    array.sorted(by: { [unowned self] in return $0 < $1 })

此网站并非旨在详尽列出所有可能使用的闭包。
参考:http://goshdarnclosuresyntax.com/