我是Swift的新手。我一直在做Java编程。我有一个在Swift中编写代码的场景。
以下代码使用Java。我需要在Swift中为以下场景编写代码
// With String array - strArr1
String strArr1[] = {"Some1","Some2"}
String strArr2[] = {"Somethingelse1","Somethingelse2"}
for( int i=0;i< strArr1.length;i++){
System.out.println(strArr1[i] + " - "+ strArr2[i]);
}
我在swift中有几个数组
var strArr1: [String] = ["Some1","Some2"]
var strArr2: [String] = ["Somethingelse1","Somethingelse2"]
for data in strArr1{
println(data)
}
for data in strArr2{
println(data)
}
// I need to loop over in single for loop based on index.
请您根据索引
提供有关循环语法的帮助答案 0 :(得分:117)
您可以使用zip()
创建
来自两个给定序列的一对序列:
let strArr1 = ["Some1", "Some2"]
let strArr2 = ["Somethingelse1", "Somethingelse2"]
for (e1, e2) in zip(strArr1, strArr2) {
print("\(e1) - \(e2)")
}
序列仅枚举给定序列/数组的“共同元素”。如果他们有不同的长度,那么额外的 简单地忽略较长数组/序列的元素。
答案 1 :(得分:16)
试试这个:
zip([0,2,4,6], [1,3,5,7]).forEach {
print($0,$1)
}
zip([0,2,4,6], [1,3,5,7]).forEach {
print($0.0,$0.1)
}
答案 2 :(得分:11)
您还可以在一个数组上enumerate
并使用索引查看第二个数组:
Swift 1.2:
for (index, element) in enumerate(strArr1) {
println(element)
println(strArr2[index])
}
斯威夫特2:
for (index, element) in strArr1.enumerate() {
print(element)
print(strArr2[index])
}
斯威夫特3:
for (index, element) in strArr1.enumerated() {
print(element)
print(strArr2[index])
}
答案 3 :(得分:9)
使用Swift 5,您可以使用以下4种Playground代码之一来解决您的问题。
zip(_:_:)
函数在最简单的情况下,您可以使用zip(_:_:)
创建初始数组中元素对的新序列(元组)。
let strArr1 = ["Some1", "Some2", "Some3"]
let strArr2 = ["Somethingelse1", "Somethingelse2"]
let sequence = zip(strArr1, strArr2)
for (el1, el2) in sequence {
print("\(el1) - \(el2)")
}
/*
prints:
Some1 - Somethingelse1
Some2 - Somethingelse2
*/
Array
的{{3}}方法和一个while循环使用简单的while循环和迭代器,同时遍历两个数组也很容易:
let strArr1 = ["Some1", "Some2", "Some3"]
let strArr2 = ["Somethingelse1", "Somethingelse2"]
var iter1 = strArr1.makeIterator()
var iter2 = strArr2.makeIterator()
while let el1 = iter1.next(), let el2 = iter2.next() {
print("\(el1) - \(el2)")
}
/*
prints:
Some1 - Somethingelse1
Some2 - Somethingelse2
*/
makeIterator()
在某些情况下,您可能需要创建自己的类型,以将姓名首字母数组的元素配对。通过使您的类型符合IteratorProtocol
,可以实现此目的。请注意,通过使您的类型也符合Sequence
协议,您可以在for循环中直接使用其实例:
struct TupleIterator: Sequence, IteratorProtocol {
private var firstIterator: IndexingIterator<[String]>
private var secondIterator: IndexingIterator<[String]>
init(firstArray: [String], secondArray: [String]) {
self.firstIterator = firstArray.makeIterator()
self.secondIterator = secondArray.makeIterator()
}
mutating func next() -> (String, String)? {
guard let el1 = firstIterator.next(), let el2 = secondIterator.next() else { return nil }
return (el1, el2)
}
}
let strArr1 = ["Some1", "Some2", "Some3"]
let strArr2 = ["Somethingelse1", "Somethingelse2"]
let tupleSequence = TupleIterator(firstArray: strArr1, secondArray: strArr2)
for (el1, el2) in tupleSequence {
print("\(el1) - \(el2)")
}
/*
prints:
Some1 - Somethingelse1
Some2 - Somethingelse2
*/
IteratorProtocol
作为上一个示例的替代方法,您可以使用AnyIterator
。以下代码显示了它在Array
扩展方法中的可能实现:
extension Array {
func pairWithElements(of array: Array) -> AnyIterator<(Element, Element)> {
var iter1 = self.makeIterator()
var iter2 = array.makeIterator()
return AnyIterator({
guard let el1 = iter1.next(), let el2 = iter2.next() else { return nil }
return (el1, el2)
})
}
}
let strArr1 = ["Some1", "Some2", "Some3"]
let strArr2 = ["Somethingelse1", "Somethingelse2"]
let iterator = strArr1.pairWithElements(of: strArr2)
for (el1, el2) in iterator {
print("\(el1) - \(el2)")
}
/*
prints:
Some1 - Somethingelse1
Some2 - Somethingelse2
*/
答案 4 :(得分:0)
如果您仍想使用Range
,则可以使用for in
。
var strArr1: [String] = ["Some1","Some2"]
var strArr2: [String] = ["Somethingelse1","Somethingelse2"]
for i in Range(start: 0, end: strArr1.count) {
println(strArr1[i] + " - " + strArr2[i])
}
答案 5 :(得分:0)
> Incase of unequal count
let array1 = ["some1","some2"]
let array2 = ["some1","some2","some3"]
var iterated = array1.makeIterator()
let finalArray = array2.map({
let itemValue = iterated.next()
return "\($0)\(itemValue != nil ? "-"+itemValue! : EmptyString)" })
//结果:[“ some1-some1”,“ some2-some2”,“ some3”]
答案 6 :(得分:-1)
for(var i = 0; i < strArr1.count ; i++)
{
println(strArr1[i] + strArr2[i])
}
应该这样做。从来没有使用过swift,所以一定要测试。
更新到最近的Swift语法
for i in 0..< strArr1.count {
print(strArr1[i] + strArr2[i])
}