我是新来的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.
请您提供您的帮助根据索引循环的语法
你可以使用zip(),它创建
来自两个给定序列的对的序列:
来自两个给定序列的对的序列:
let strArr1 = ["Some1","Some2"] let strArr2 = ["Somethingelse1","Somethingelse2"] for (e1,e2) in zip(strArr1,strArr2) { print("\(e1) - \(e2)") }
该序列仅列举给定序列/阵列的“共同元件”。如果他们有不同的长度,那么额外简单地忽略较长阵列/序列的元素。