Adèle Dunand Adèle Dunand - 1 year ago 67
Swift Question

Array (class) filled with non nil values stays empty

I am currently having trouble filling up an array of customClass.

I try to fill it with a jsonFile. During my json parsing (using swiftyJSON) i loop and fill my array.
The problem is, at the end of my loop, it is still empty. I tested it in different ways, and here is my code:

enter image description here

That's the file where the problem is. In my loop I fill an Annotation, that I add with append to my array. The problem is what my print return. Here is a part of it:

enter image description here

It's just a small part of a huge jsonfile. And, my is correctly printed every iteration. But when it comes to my Array, nothing.

So I'm completly lost and hope you could help me ^^

(And for the information, here is my custom class) :

enter image description here

And btw, I tried to print my array.count, and it's nil too

Im so sorry if the question has been posted. I couldn't find it in the entire website.

Answer Source

Change your JSONAnnotationList declaration to be an non-optional and assign it an empty array

var JSONAnnotationList: [UGOAnnotation] = []

You see, you have never created an array so there was nothing to be printed.

The whole point of optionals is to use them sparingly, not everywhere.