如何在Swift中的另一个数组中追加一个数组? [英] How to append an array in another array in Swift?

查看:594
本文介绍了如何在Swift中的另一个数组中追加一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a JSON response whose answer I have to parse. I write the single elements into an array called courseDataArray using a for loop. After that, I want to write this newly created array into another array called combinedCourseArray with the aim to pass that on to a UITableView. Creating the first array seems to work fine.

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

But how can I create another array combinedCourseArray who contain all arrays of type 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)

            }

推荐答案

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

You should move the combinedCourseArray declaration outside of the array. It should be var combinedCourseArray: [[String: AnyObject]] = [[:]] since it's an array and not a dictionary.

你应该做的

combinedCourseArray.append(courseDataArray)

代替

combinedCourseArray[0] = courseDataArray

这篇关于如何在Swift中的另一个数组中追加一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆