How to add an array to another array in Swift?

I have a JSON response whose answer I have to parse. I am writing individual elements to an array named courseDataArrayusing for loop. After that, I want to write this newly created array to another array with the name combinedCourseArrayin order to pass this to UITableView. Creating the first array works fine.

But how can I create another array combinedCourseArraythat contains 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)

            }
+4
source share
4 answers

You must transfer the declaration of the combined Competition outside the array. It should be var combinedCourseArray: [[String: AnyObject]] = [[:]], as it is an array, not a dictionary.

And you must do

combinedCourseArray.append(courseDataArray)

instead

combinedCourseArray[0] = courseDataArray
+5
var FirstArray = [String]()
var SecondArray = [String:AnyObject]()

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

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

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"]
    ]
}

Of course, protection is not needed, since the input type is supposedly narrower [[String:AnyObject]], and since then you cannot have internal failures, you can simply use mapinsteadflatMap

+1
source

Source: https://habr.com/ru/post/1649308/


All Articles