如何提取单个集合中的多个集合,以在SWIFT中成为它们自己的单独数组?

0yycz8jy  于 2022-10-04  发布在  Swift
关注(0)|答案(1)|浏览(148)
let groupA : Set<Int> = [1111,2000]
let groupB : Set<Int> = [221,122]
let groupC : Set<Int> = [300,12,23,232] 

StudentGroups = [groupA, groupB, groupC]

假设我无法访问上面声明的集合,我正在尝试找到一种解决方案来提取StudentGroups中的每个集合,以成为它们自己的新数组。

例如:预计从StudentGroups中提取的数组应为:

groupA = [1111,2000]
groupB = [221,122]
groupC = [300,12,23, 232]

这就是我为了测试自己的进步所做的:

for idx in StudentGroups.indices {
   let elem = StudentGroups[idx]
    print(elem)

//This will return  [1111,2000]
//                  [221,122]
//                  [300,12,23,232]

}

目前,它只在集合中迭代。不幸的是,我对如何继续下去一无所知

w46czmvw

w46czmvw1#

试着这样做:

let groupA : Set<Int> = [1111,2000]
let groupB : Set<Int> = [221,122]
let groupC : Set<Int> = [300,12,23,232]

let studentGroups = [groupA, groupB, groupC]
print("---> studentGroups: (studentGroups)")

for idx in studentGroups.indices {
    let arr = Array(studentGroups[idx])
    print("n---> arr (idx): (arr)")
    for i in arr.indices {
        print("     ---> element (i) of arr (idx): (arr[i])")
    }
}

...a solution to extract out each sets inside StudentGroups to become their new own array...,试试这个,将集合的数组转换为数组的数组:

var newArr: [[Int]] = studentGroups.map{ Array(B1a1a1b) }

print("n---> newArr: (newArr)")
print("n---> newArr[0][0]: (newArr[0][0])")

相关问题