如何在Swift中的另一个数组中附加数组?

时间:2016-07-27 15:59:24

标签: arrays swift multidimensional-array

我有一个JSON响应,我必须解析它的答案。我使用courseDataArray将单个元素写入名为for loop的数组中。之后,我想将这个新创建的数组写入另一个名为combinedCourseArray的数组中,目的是将其传递给UITableView。创建第一个数组似乎工作正常。

但是如何创建另一个包含所有combinedCourseArray类型数组的数组courseDataArray

for (index, element) in result.enumerate() {

            // get one entry from the result array
            if let courseEntry = result[index] as? [String:AnyObject]{

                //work with the content of the array
                let courseName = courseEntry["name"]
                let courseType = courseEntry["course_type"]
                let courseDate = courseEntry["cor_date"]
                let courseId = courseEntry["cor_id"]
                let duration = courseEntry["duration"]
                let schoolId = courseEntry["sco_id"]
                let status = courseEntry["status"]


                let courseDataArray = ["courseName" : courseName, "courseType": courseType, "courseDate": courseDate, "courseId": courseId, "duration": duration, "schoolId":schoolId, "status":status]

                print(courseDataArray)

                var combinedCourseArray: [String: AnyObject] = [:]
                combinedCourseArray[0] = courseDataArray //does not work -- error: cannot subscript a value of type...

               // self.shareData.courseStore.append(scooter)

            }

4 个答案:

答案 0 :(得分:5)

您应该将combinedCourseArray声明移到数组之外。它应该是var combinedCourseArray: [[String: AnyObject]] = [[:]],因为它是一个数组而不是字典。

你应该这样做

combinedCourseArray.append(courseDataArray)

而不是

combinedCourseArray[0] = courseDataArray

答案 1 :(得分:5)

var FirstArray = [String]()
var SecondArray = [String:AnyObject]()

FirstArray.append(contentsOf: SecondArray.value(forKey: "key") as! [String])

答案 2 :(得分:3)

首先在此循环中声明此combinedCourseArray数组

var combinedCourseArray: [[String: AnyObject]] = [[String: AnyObject]]()
for (index, element) in result.enumerate() {

        // get one entry from the result array
        if let courseEntry = result[index] as? [String:AnyObject]{

            //work with the content of the array
            let courseName = courseEntry["name"]
            let courseType = courseEntry["course_type"]
            let courseDate = courseEntry["cor_date"]
            let courseId = courseEntry["cor_id"]
            let duration = courseEntry["duration"]
            let schoolId = courseEntry["sco_id"]
            let status = courseEntry["status"]


            let courseDataArray = ["courseName" : courseName, "courseType": courseType, "courseDate": courseDate, "courseId": courseId, "duration": duration, "schoolId":schoolId, "status":status]

            print(courseDataArray)


            combinedCourseArray.append(courseDataArray) //does not work -- error: cannot subscript a value of type...

           // self.shareData.courseStore.append(scooter)

        }
 }

答案 3 :(得分:1)

只需在外部数组上使用flatMap将一个数组转换为另一个数组,可能会删除一些元素:

let courseDataArray : [[String:AnyObject?]] = result.flatMap {
    guard let courseEntry = $0 as? [String:AnyObject] else {
        return nil
    }

    return [
        "courseName" : courseEntry["name"],
        "courseType": courseEntry["course_type"],
        "courseDate": courseEntry["cor_date"],
        "courseId": courseEntry["cor_id"],
        "duration": courseEntry["duration"],
        "schoolId": courseEntry["sco_id"],
        "status": courseEntry["status"]
    ]
}

当然,由于输入类型可能已经[[String:AnyObject]],并且由于您无法解决任何内部故障,所以后卫并非真正必要,您可以使用map而不是flatMap