-1

as my last Question was marked a a duplicate i am going to rephrase it and give an example:

i want to create a list with a variable amount of packages, where each package consists of a name, 2 coordinates and some other information. I have an algorithm which sets an annotation on a map for each of those packages.

The problem i have is, that i need to write a new algorithm for each annotation. So i thought of using a for-loop which inserts the information inside of each package into the algorithm and repeats that for every single package. The problem here was so far that i would need more than one condition serving the for-loop which didnt work out for me. It is also quite difficult to adress the different parts of information inside of the packages specifically.

I hope it gets clearer now what i meant and someone has an idea how to solve the problem.

pacification
  • 5,838
  • 4
  • 29
  • 51
Gustulus
  • 33
  • 7

1 Answers1

2

Use zip like suggested by Martin. You can then iterate over each element of the array at the same time.

let x = [0, 1, 2]
let y = [5, 4, 3]

for (_x, _y) in zip(x, y) {
    print("pair: \(_x) \(_y)")
}
Andy Ibanez
  • 12,104
  • 9
  • 65
  • 100
  • There is no need to use stride here. `for i in 0.. – Martin R Aug 31 '18 at 22:09
  • Those are two extra ways I can add to my answer then :-) – Andy Ibanez Aug 31 '18 at 22:10
  • hmm not really what i was Looking for – Gustulus Aug 31 '18 at 22:58
  • @Gustulus lol You'll have to give us a bit more feedback then that if you're going to expect someone to be able to help you – Alexander Aug 31 '18 at 22:59
  • i will try to explain my idea a little better – Gustulus Aug 31 '18 at 22:59
  • @AndyIbanez I recommend to remove all by the `zip` solution. There's absolutely no reason to be using the other 3 ways. – Alexander Aug 31 '18 at 23:00
  • You may need to clarify your question then. Based on the description and attempt you write with that `for`, it looks like to me you want to iterate through both arrays at the same time. – Andy Ibanez Aug 31 '18 at 23:00
  • so my idea is to have a specific number of packages which all consist or a title and 3-4 values (a mix of numbers and strings) – Gustulus Aug 31 '18 at 23:01
  • and now i have an algorithm which uses these packages each for themself (first it prints the name and then it uses the other values) and i want this algorithm to do this procedure on every single package so in the end i have a list of all the packages – Gustulus Aug 31 '18 at 23:03
  • let me give you an example: – Gustulus Aug 31 '18 at 23:03
  • you have a map and you want to set annotations and now you create a list of "packages" which all consist of the specific name of this Annotation, the coordinates and some other random information – Gustulus Aug 31 '18 at 23:05
  • and now i want my algorithm (which creates the annotations and sorts out the layout and stuff) to use these "annotation-packages" and set annotations at the given coordinates with all the Information given in each"package" – Gustulus Aug 31 '18 at 23:07
  • i hope it gets a little bit clearer now – Gustulus Aug 31 '18 at 23:07