I'm trying to add a tuple (e.g., 2-item tuple) to an array.
我正在尝试向数组中添加一个tuple(例如,2项tuple)。
var myStringArray: (String,Int)[]? = nil
myStringArray += ("One", 1)
What I'm getting is:
我得到的是:
Could not find an overload for '+=' that accepts the supplied arguments
无法找到接受所提供参数的'+='的重载
Hint: I tried to do an overload of the '+=' per reference book:
提示:我试着把每本参考书的“+=”写得太多了:
@assignment func += (inout left: (String,Int)[], right: (String,Int)[]) {
left = (left:String+right:String, left:Int+right+Int)
}
...but haven't got it right.
…但我没做对。
Any ideas? ...solution?
什么好主意吗?解决方案?
8 个解决方案
#1
96
Since this is still the top answer on google for adding tuples to an array, its worth noting that things have changed slightly in the latest release. namely:
由于这仍然是谷歌中向数组添加元组的最佳答案,因此值得注意的是,在最近的版本中,情况略有变化。即:
when declaring/instantiating arrays; the type is now nested within the braces:
当声明/实例化数组;该类型现在嵌套在大括号内:
var stuff:[(name: String, value: Int)] = []
the compound assignment operator, +=
, is now used for concatenating arrays; if adding a single item, it needs to be nested in an array:
复合赋值运算符+=现在用于连接数组;如果添加一个项目,则需要嵌套在数组中:
stuff += [(name: "test 1", value: 1)]
it also worth noting that when using append()
on an array containing named tuples, you can provide each property of the tuple you're adding as an argument to append()
:
同样值得注意的是,当在包含命名元组的数组中使用append()时,您可以向append()添加作为参数的元组的每个属性:
stuff.append((name: "test 2", value: 2))
#2
9
You have two issues. First problem, you're not creating an "array of tuples", you're creating an "optional array of tuples". To fix that, change this line:
你有两个问题。第一个问题,您不是在创建一个“元组数组”,而是在创建一个“可选的元组数组”。要解决这个问题,请改变这条线:
var myStringArray: (String,Int)[]? = nil
to:
:
var myStringArray: (String,Int)[]
Second, you're creating a variable, but not giving it a value. You have to create a new array and assign it to the variable. To fix that, add this line after the first one:
第二,创建一个变量,但不给它赋值。您必须创建一个新的数组并将其分配给变量。要解决这个问题,请在第一个后面加上这条线:
myStringArray = []
...or you can just change the first line to this:
…或者你可以把第一行改成这样
var myStringArray: (String,Int)[] = []
After that, this line works fine and you don't have to worry about overloading operators or other craziness. You're done!
在那之后,这条线就可以正常工作了,你不必担心操作符重载或其他疯狂的事情。你已经完成了!
myStringArray += ("One", 1)
Here's the complete solution. A whopping two lines and one wasn't even changed:
这是完整的解决方案。多达两行和一行都没有改变:
var myStringArray: (String,Int)[] = []
myStringArray += ("One", 1)
#3
3
If you remove the optional, it works fine, otherwise you'll have to do this:
如果你删除了可选选项,它可以正常工作,否则你必须这样做:
var myStringArray: (String,Int)[]? = nil
if !myStringArray {
myStringArray = []
}
var array = myStringArray!
array += ("One", 1)
myStringArray = array
You can never append an empty array, so you'll have to initialize it at some point. You'll see in the overload operator below that we sort of lazy load it to make sure that it is never nil.
您永远不能附加一个空数组,因此您必须在某个时刻初始化它。你会在下面的重载操作符中看到我们有点懒加载它以确保它永远不是nil。
You could condense this into a '+=' operator:
您可以将其压缩为'+='运算符:
@assignment func += (inout left: Array<(String, Int)>?, right: (String, Int)) {
if !left {
left = []
}
var array = left!
array.append(right.0, right.1)
left = array
}
Then call:
然后调用:
var myStringArray: (String,Int)[]? = nil
myStringArray += ("one", 1)
#4
2
I've ended up here multiple times due to this issue. Still not as easy as i'd like with appending onto an array of tuples. Here is an example of how I do it now.
由于这个问题,我已经在这里呆过好几次了。仍然不像我希望的那样简单,添加到元组数组中。这是我现在做的一个例子。
Set an alias for the Tuple - key point
为元组键点设置别名
typealias RegionDetail = (regionName:String, constraintDetails:[String:String]?)
Empty array
空数组
var allRegionDetails = [RegionDetail]()
Easy to add now
容易把现在
var newRegion = RegionDetail(newRegionName, constraints)
allRegionDetails.append(newRegion)
var anotherNewRegion = RegionDetail("Empty Thing", nil)
allRegionDetails.append(anotherNewRegion)
#5
1
Note: It's not work anymore if you do:
注意:如果你这样做了,它就不再工作了:
array += tuple
you will get error what you need is :
你会得到错误,你需要的是:
array += [tuple]
I think apple change to this representation because it's more logical
我认为苹果改变了这种表述,因为它更符合逻辑
#6
1
Swift 4 solution:
斯威夫特4解决方案:
// init empty tuple array
var myTupleArray: [(String, Int)] = []
// append a value
myTupleArray.append(("One", 1))
#7
0
@assignment func += (inout left: Array<(String, Int)>?, right: (String, Int)) {
if !left {
left = []
}
if left {
var array = left!
array.append(right.0, right.1)
left = array
}
}
var myStringArray: (String,Int)[]? = nil
myStringArray += ("x",1)
#8
0
Thanks to comments:
由于评论:
import UIKit
@assignment func += (inout left: Array<(String, Int)>?, right: (String, Int)) {
if !left {
left = []
}
if left {
var array = left!
array.append(right.0, right.1)
left = array
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let interestingNumbers = [
"Prime": [2, 3, 5, 7, 11, 13],
"Fibonacci": [1, 1, 2, 3, 5, 8],
"Square": [1, 4, 9, 16, 25],
]
println("interestingNumbers: \(interestingNumbers)\n")
var largest = 0
var myStringArray: (String,Int)[]? = nil
myStringArray += ("One", 1)
var x = 0
for (kind, numbers) in interestingNumbers {
println(kind)
for number in numbers {
if number > largest {
largest = number
}
x++
println("\(x)) Number: \(number)")
myStringArray += (kind,number)
} // end Number
} // end Kind
println("myStringArray: \(myStringArray)")
}
}
The Output:
输出:
interestingNumbers: [Square: [1, 4, 9, 16, 25], Prime: [2, 3, 5, 7, 11, 13], Fibonacci: [1, 1, 2, 3, 5, 8]]
有趣的数字:[方:[1,4,9,16,25],素数:[2,3,5,7,11,13],斐波那契:[1,1,2,3,5,8]]
Square
1) Number: 1
2) Number: 4
3) Number: 9
4) Number: 16
5) Number: 25
Prime
6) Number: 2
7) Number: 3
8) Number: 5
9) Number: 7
10) Number: 11
11) Number: 13
Fibonacci
12) Number: 1
13) Number: 1
14) Number: 2
15) Number: 3
16) Number: 5
17) Number: 8广场1)数量:1 2)数量:4 3)数量:9 4)数量:16 5)数量:25 ' 6)数量:2 7)数量:3 8)数量:5 9)数量:7 10)数量:11 11)数量:13个斐波那契12)数量:1 13)数量:1 14)数量:2 15)数量:3 16)数量:5 17)数量:8
Array of tupules:
tupules数组:
myStringArray: [(One, 1), (Square, 1), (Square, 4), (Square, 9), (Square, 16), (Square, 25), (Prime, 2), (Prime, 3), (Prime, 5), (Prime, 7), (Prime, 11), (Prime, 13), (Fibonacci, 1), (Fibonacci, 1), (Fibonacci, 2), (Fibonacci, 3), (Fibonacci, 5), (Fibonacci, 8)]
myStringArray: [(1, 1) (1) (Square, 4) (Square, 9) (Square, 16) (Square, 16) (Square, 25) (Prime, 2) (Prime, 3) (Prime, 5) (Prime, 7) (Prime, 11)
#1
96
Since this is still the top answer on google for adding tuples to an array, its worth noting that things have changed slightly in the latest release. namely:
由于这仍然是谷歌中向数组添加元组的最佳答案,因此值得注意的是,在最近的版本中,情况略有变化。即:
when declaring/instantiating arrays; the type is now nested within the braces:
当声明/实例化数组;该类型现在嵌套在大括号内:
var stuff:[(name: String, value: Int)] = []
the compound assignment operator, +=
, is now used for concatenating arrays; if adding a single item, it needs to be nested in an array:
复合赋值运算符+=现在用于连接数组;如果添加一个项目,则需要嵌套在数组中:
stuff += [(name: "test 1", value: 1)]
it also worth noting that when using append()
on an array containing named tuples, you can provide each property of the tuple you're adding as an argument to append()
:
同样值得注意的是,当在包含命名元组的数组中使用append()时,您可以向append()添加作为参数的元组的每个属性:
stuff.append((name: "test 2", value: 2))
#2
9
You have two issues. First problem, you're not creating an "array of tuples", you're creating an "optional array of tuples". To fix that, change this line:
你有两个问题。第一个问题,您不是在创建一个“元组数组”,而是在创建一个“可选的元组数组”。要解决这个问题,请改变这条线:
var myStringArray: (String,Int)[]? = nil
to:
:
var myStringArray: (String,Int)[]
Second, you're creating a variable, but not giving it a value. You have to create a new array and assign it to the variable. To fix that, add this line after the first one:
第二,创建一个变量,但不给它赋值。您必须创建一个新的数组并将其分配给变量。要解决这个问题,请在第一个后面加上这条线:
myStringArray = []
...or you can just change the first line to this:
…或者你可以把第一行改成这样
var myStringArray: (String,Int)[] = []
After that, this line works fine and you don't have to worry about overloading operators or other craziness. You're done!
在那之后,这条线就可以正常工作了,你不必担心操作符重载或其他疯狂的事情。你已经完成了!
myStringArray += ("One", 1)
Here's the complete solution. A whopping two lines and one wasn't even changed:
这是完整的解决方案。多达两行和一行都没有改变:
var myStringArray: (String,Int)[] = []
myStringArray += ("One", 1)
#3
3
If you remove the optional, it works fine, otherwise you'll have to do this:
如果你删除了可选选项,它可以正常工作,否则你必须这样做:
var myStringArray: (String,Int)[]? = nil
if !myStringArray {
myStringArray = []
}
var array = myStringArray!
array += ("One", 1)
myStringArray = array
You can never append an empty array, so you'll have to initialize it at some point. You'll see in the overload operator below that we sort of lazy load it to make sure that it is never nil.
您永远不能附加一个空数组,因此您必须在某个时刻初始化它。你会在下面的重载操作符中看到我们有点懒加载它以确保它永远不是nil。
You could condense this into a '+=' operator:
您可以将其压缩为'+='运算符:
@assignment func += (inout left: Array<(String, Int)>?, right: (String, Int)) {
if !left {
left = []
}
var array = left!
array.append(right.0, right.1)
left = array
}
Then call:
然后调用:
var myStringArray: (String,Int)[]? = nil
myStringArray += ("one", 1)
#4
2
I've ended up here multiple times due to this issue. Still not as easy as i'd like with appending onto an array of tuples. Here is an example of how I do it now.
由于这个问题,我已经在这里呆过好几次了。仍然不像我希望的那样简单,添加到元组数组中。这是我现在做的一个例子。
Set an alias for the Tuple - key point
为元组键点设置别名
typealias RegionDetail = (regionName:String, constraintDetails:[String:String]?)
Empty array
空数组
var allRegionDetails = [RegionDetail]()
Easy to add now
容易把现在
var newRegion = RegionDetail(newRegionName, constraints)
allRegionDetails.append(newRegion)
var anotherNewRegion = RegionDetail("Empty Thing", nil)
allRegionDetails.append(anotherNewRegion)
#5
1
Note: It's not work anymore if you do:
注意:如果你这样做了,它就不再工作了:
array += tuple
you will get error what you need is :
你会得到错误,你需要的是:
array += [tuple]
I think apple change to this representation because it's more logical
我认为苹果改变了这种表述,因为它更符合逻辑
#6
1
Swift 4 solution:
斯威夫特4解决方案:
// init empty tuple array
var myTupleArray: [(String, Int)] = []
// append a value
myTupleArray.append(("One", 1))
#7
0
@assignment func += (inout left: Array<(String, Int)>?, right: (String, Int)) {
if !left {
left = []
}
if left {
var array = left!
array.append(right.0, right.1)
left = array
}
}
var myStringArray: (String,Int)[]? = nil
myStringArray += ("x",1)
#8
0
Thanks to comments:
由于评论:
import UIKit
@assignment func += (inout left: Array<(String, Int)>?, right: (String, Int)) {
if !left {
left = []
}
if left {
var array = left!
array.append(right.0, right.1)
left = array
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let interestingNumbers = [
"Prime": [2, 3, 5, 7, 11, 13],
"Fibonacci": [1, 1, 2, 3, 5, 8],
"Square": [1, 4, 9, 16, 25],
]
println("interestingNumbers: \(interestingNumbers)\n")
var largest = 0
var myStringArray: (String,Int)[]? = nil
myStringArray += ("One", 1)
var x = 0
for (kind, numbers) in interestingNumbers {
println(kind)
for number in numbers {
if number > largest {
largest = number
}
x++
println("\(x)) Number: \(number)")
myStringArray += (kind,number)
} // end Number
} // end Kind
println("myStringArray: \(myStringArray)")
}
}
The Output:
输出:
interestingNumbers: [Square: [1, 4, 9, 16, 25], Prime: [2, 3, 5, 7, 11, 13], Fibonacci: [1, 1, 2, 3, 5, 8]]
有趣的数字:[方:[1,4,9,16,25],素数:[2,3,5,7,11,13],斐波那契:[1,1,2,3,5,8]]
Square
1) Number: 1
2) Number: 4
3) Number: 9
4) Number: 16
5) Number: 25
Prime
6) Number: 2
7) Number: 3
8) Number: 5
9) Number: 7
10) Number: 11
11) Number: 13
Fibonacci
12) Number: 1
13) Number: 1
14) Number: 2
15) Number: 3
16) Number: 5
17) Number: 8广场1)数量:1 2)数量:4 3)数量:9 4)数量:16 5)数量:25 ' 6)数量:2 7)数量:3 8)数量:5 9)数量:7 10)数量:11 11)数量:13个斐波那契12)数量:1 13)数量:1 14)数量:2 15)数量:3 16)数量:5 17)数量:8
Array of tupules:
tupules数组:
myStringArray: [(One, 1), (Square, 1), (Square, 4), (Square, 9), (Square, 16), (Square, 25), (Prime, 2), (Prime, 3), (Prime, 5), (Prime, 7), (Prime, 11), (Prime, 13), (Fibonacci, 1), (Fibonacci, 1), (Fibonacci, 2), (Fibonacci, 3), (Fibonacci, 5), (Fibonacci, 8)]
myStringArray: [(1, 1) (1) (Square, 4) (Square, 9) (Square, 16) (Square, 16) (Square, 25) (Prime, 2) (Prime, 3) (Prime, 5) (Prime, 7) (Prime, 11)