如何在Swift中创建一个元组数组,其中元组中的一个项是可选的?

时间:2022-12-28 00:33:11

Take the following code:

请使用以下代码:

var tupleArray: [(firstName: String, middleName: String?)] = []
tupleArray.append(firstName: "Bob", middleName: nil)
tupleArray.append(firstName: "Tom", middleName: "Smith") // causes an error

I want an array of tuples consisting of a first name and a middle name, the middle name can be nil or have a value (thus, optional). However with the above creation code the third line gives me an error. Why? How do I get around this?

我想要一个由名字和中间名组成的元组数组,中间名可以是nil或有一个值(因此,可选)。但是使用上面的创建代码,第三行给出了一个错误。为什么?我该如何解决这个问题?

3 个解决方案

#1


11  

This could be a compiler bug. As in How do I create an array of tuples?, you can define a type alias as a workaround:

这可能是编译器错误。如在如何创建元组数组?中,您可以将类型别名定义为变通方法:

typealias NameTuple = (firstName: String, middleName: String?)

var tupleArray: [NameTuple] = []
tupleArray.append( (firstName: "Bob", middleName: nil) )
tupleArray.append( (firstName: "Tom", middleName: "Smith") )

#2


0  

Another workaround would be making Optional<String>.Some explicitly.

另一种解决方法是显式地使Optional .Some。

var tupleArray: [(firstName: String, middleName: String?)] = []
tupleArray.append(firstName: "Bob", middleName: nil)
tupleArray.append(firstName: "Tom", middleName: .Some("Smith"))

#3


-1  

Something Intresting.It seems to be as some bug but we can add using some other syntax try using following way

有趣的东西。它似乎是一些bug,但我们可以使用其他语法添加尝试使用以下方式

var pfc : [(prime: Int, count: Int)] = []

pfc.append(prime: 2, count: 2)

pfc += [(prime: 3, count: 4)]

var p = 5
var c = 1

var tuple = (prime: p, count: c)

pfc += [tuple]

for optinal variables go through the following code (originally provided by Martin R) it works fine for me

对于optinal变量,请通过以下代码(最初由Martin R提供),它对我来说很好

    typealias tupleArray =  (firstName: String, middleName: String?)

    var fun1: [tupleArray] = [tupleArray]()
    fun1.append((firstName: "Bob", middleName: nil))
    fun1.append((firstName: "Tom", middleName: "Smith"))
    println(fun1)

#1


11  

This could be a compiler bug. As in How do I create an array of tuples?, you can define a type alias as a workaround:

这可能是编译器错误。如在如何创建元组数组?中,您可以将类型别名定义为变通方法:

typealias NameTuple = (firstName: String, middleName: String?)

var tupleArray: [NameTuple] = []
tupleArray.append( (firstName: "Bob", middleName: nil) )
tupleArray.append( (firstName: "Tom", middleName: "Smith") )

#2


0  

Another workaround would be making Optional<String>.Some explicitly.

另一种解决方法是显式地使Optional .Some。

var tupleArray: [(firstName: String, middleName: String?)] = []
tupleArray.append(firstName: "Bob", middleName: nil)
tupleArray.append(firstName: "Tom", middleName: .Some("Smith"))

#3


-1  

Something Intresting.It seems to be as some bug but we can add using some other syntax try using following way

有趣的东西。它似乎是一些bug,但我们可以使用其他语法添加尝试使用以下方式

var pfc : [(prime: Int, count: Int)] = []

pfc.append(prime: 2, count: 2)

pfc += [(prime: 3, count: 4)]

var p = 5
var c = 1

var tuple = (prime: p, count: c)

pfc += [tuple]

for optinal variables go through the following code (originally provided by Martin R) it works fine for me

对于optinal变量,请通过以下代码(最初由Martin R提供),它对我来说很好

    typealias tupleArray =  (firstName: String, middleName: String?)

    var fun1: [tupleArray] = [tupleArray]()
    fun1.append((firstName: "Bob", middleName: nil))
    fun1.append((firstName: "Tom", middleName: "Smith"))
    println(fun1)