4

Hi guys im been had an issue with this kind of array, so basically i want to pass data an array to my server which like this :

{adult=[["Mr", "Benedict Cumberbatch", "07", "September", "1989", "null", "0", "null", "null", "null", "null", "null"],["Mr", "Tony Stark", "17", "July", "1996", "null", "0", "null", "null", "null", "null", "null"]]}

and i have array like this

["Mr", "Benedict Cumberbatch", "07", "September", "1989", "null", "0", "null", "null", "null", "null", "null"]

and this

["Mr", "Tony Stark", "17", "July", "1996", "null", "0", "null", "null", "null", "null", "null"]

i create that with this code :

    PassengerModel.passenger_array_json.append(title)
    PassengerModel.passenger_array_json.append(name)
    PassengerModel.passenger_array_json.append(date!)
    PassengerModel.passenger_array_json.append(month!)
    PassengerModel.passenger_array_json.append(year!)
    PassengerModel.passenger_array_json.append("null")
    PassengerModel.passenger_array_json.append("0")
    PassengerModel.passenger_array_json.append("null")
    PassengerModel.passenger_array_json.append("null")
    PassengerModel.passenger_array_json.append("null")
    PassengerModel.passenger_array_json.append("null")
    PassengerModel.passenger_array_json.append("null")

so i had an idea, if i append array to an array will give me that result, and il try this :

PassengerModel.passenger_array_json_2.append(PassengerModel.passenger_array_json)

but it give me an error, anybody know, somehow i can create data like this :

["Mr", "Benedict Cumberbatch", "07", "September", "1989", "null", "0", "null", "null", "null", "null", "null"],["Mr", "Tony Stark", "17", "July", "1996", "null", "0", "null", "null", "null", "null", "null"]
4

1 Answer 1

5

What exactly is your code?

let sherlock = ["Mr", "Benedict Cumberbatch"] //Implicit type of sherlock: [String] (or Array<String> if you want)
let ironman = ["Mr", "Tony Stark"]
var characters: [[String]] = [] //Here you create an array of arrays, so one entry for each superhero
characters.append(sherlock)
characters.append(ironman)

works. You can't append ironman to sherlock, because sherlock is an array containing only strings and the result would be ["Mr", "Benedict Cumberbatch", ["Mr", "Tony Stark"]] (which would not be what you wanted anyways and is not an array of strings anymore (the third entry is an Array...)). So you need a new array for your actors

EDIT: For everyone who is looking for an answer to the title, here is an example from the documentation:

var numbers = [1, 2, 3, 4, 5]
numbers.append(contentsOf: 10...15)
print(numbers)
// Prints "[1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15]" here
Sign up to request clarification or add additional context in comments.

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.