4

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.

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)

            }
4
  • combinedArray should be of type [[String: AnyObject]] as it is an Array<[String:AnyObject]>. Use var combinedCourseArray: [[String: AnyObject]] = [[:]] Commented Jul 27, 2016 at 16:03
  • [String: AnyObject] is dictionary not array Commented Jul 27, 2016 at 16:03
  • Thx. that was the problem. I had to add ? after AnyObject: var combinedCourseArray: [[String: AnyObject?]] = [[:]] Commented Jul 27, 2016 at 16:27
  • BTW, courseDataArray is actually a Dictionary not an Array Commented Jul 27, 2016 at 17:32

4 Answers 4

5

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.

And you should be doing

combinedCourseArray.append(courseDataArray)

instead of

combinedCourseArray[0] = courseDataArray
Sign up to request clarification or add additional context in comments.

1 Comment

Thx. works fine. I had to add the question mark here: var combinedCourseArray: [[String: AnyObject?]] = [[:]]
5
var FirstArray = [String]()
var SecondArray = [String:AnyObject]()

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

Comments

3

First declare this combinedCourseArray array out side this loop

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)

        }
 }

1 Comment

Thx. works fine. I had to add the question mark here: var combinedCourseArray: [[String: AnyObject?]] = [[:]]
1

Just use flatMap on the outer array to translate one array into another array, possibly dropping some elements:

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, the guard isn't really necessary since the input type is presumably already [[String:AnyObject]] and since you then can't have any internal failures, you can just use map instead of flatMap

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.