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
  • Show the code for passenger model. Why are you trying to pass data to your server like this in an array? it would be much better to send it as a dictionary. Commented May 23, 2018 at 9:18
  • What about array.append(contentsOf: anotherArray) ? Commented May 23, 2018 at 9:23
  • This part: ["Mr", "Benedict Cumberbatch", "07",... is a String array. I think it would be better like { "title": "Mr", "name": "Benedict Cumberbatch" ...} i didn't expect to have to explain that one. Would be much better to map it to a Character model that conforms to codable wouldn't it Commented May 23, 2018 at 9:27
  • Also, being a web and mobile developer. I know that sending data like this, it is ordered and would have to be read in order on the server side. If a value was missed/misplaced then your data is just wrong. at least with key pairs ordering doesn't matter Commented May 23, 2018 at 9:30

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.