加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift中闭包参数的语法是什么?

发布时间:2020-12-14 04:47:56 所属栏目:百科 来源:网络整理
导读:在 Swift头文件中,isSeparator:参数接受一个闭包 public func split(maxSplit: Int = default,allowEmptySlices: Bool = default,@noescape isSeparator: (Self.Generator.Element) throws - Bool) rethrows - [Self.SubSequence] 但是在documentation中,它
在 Swift头文件中,isSeparator:参数接受一个闭包

public func split(maxSplit: Int = default,allowEmptySlices: Bool = default,@noescape isSeparator: (Self.Generator.Element) throws -> Bool) rethrows -> [Self.SubSequence]

但是在documentation中,它以不同的方式列出了闭包语法

{ (parameters) -> return type in
    statements
}

你怎么知道(Self.Generator.Element)抛出 – > Bool rethrows是指一个闭包/需要一个闭包?是否还有其他方式,标题/文档可能会将参数列为闭包?

解决方法

赠送这个封闭的“东西”是 – >.完整的类型是

(Self.Generator.Element) throws -> Bool

这意味着闭包采用Self.Generator.Element类型的变量,并且必须在基于输入的某些计算时返回Bool.这样做可能会产生一些错误 – 这就是投掷的目的.

你写的是什么

{ (parameters) -> return type in
    statements
}

将是一个实际的实现,一些通用闭包类型的值.

闭包的类型是例如(someInt:Int,someDouble:Double) – >串:

var a : ((someInt:Int,someDouble:Double) -> String)

再一次,赠送a实际上是封闭的东西是 – >在类型声明中.

然后你在第二个代码块之后通过一些代码片段分配一些内容:

a = { (integer,floating) -> String in
    return "(integer) (floating)"
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读