gpt4 book ai didi

ios - Swift 2.0 元组模式元素标签必须是 '_'

转载 作者:搜寻专家 更新时间:2023-11-01 06:07:57 25 4
gpt4 key购买 nike

自 swift 2.0 更新以来,我一直在尝试修复我的所有代码。我有一个问题,似乎是元组现在的工作方式:

public func generate() -> AnyGenerator <(String, JSON)> {
switch self.type {
case .Array:
let array_ = object as! [AnyObject]
var generate_ = array_.generate()
var index_: Int = 0
return anyGenerator{
if let element_: AnyObject = generate_.next() {
return ("\(index_++)", JSON(element_))
} else {
return nil
}
}
case .Dictionary:
let dictionary_ = object as! [String : AnyObject]
var generate_ = dictionary_.generate()
return anyGenerator{
if let (key_: String, value_: AnyObject) = generate_.next() {
return (key_, JSON(value_))
} else {
return nil
}
}
default:
return anyGenerator{
return nil
}
}
}

特别是行:

if let (key_: String, value_: AnyObject) = generate_.next()

正在抛出错误:元组模式元素标签'key'必须是'_'

我已经尝试过进行更改,但没有成功...

有什么想法吗?

最佳答案

问题是:我们不能在元组模式内部使用类型注释。

release notes :

Type annotations are no longer allowed in patterns and are considered part of the outlying declaration. This means that code previously written as:

var (a : Int, b : Float) = foo()

needs to be written as:

var (a,b) : (Int, Float) = foo()

if an explicit type annotation is needed. The former syntax was ambiguous with tuple element labels. (20167393)

所以,你可以:

if let (key_, value_): (String, AnyObject) = generate_.next() {

但在这种情况下,您可以省略 : (String, AnyObject):

if let (key_, value_) = generate_.next() {

关于ios - Swift 2.0 元组模式元素标签必须是 '_',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32642069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com