Array (class) filled with non nil values stays emp

2020-05-10 05:43发布

问题:

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:

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:

It's just a small part of a huge jsonfile. And, my tmpAnnot.name 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) :

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.

回答1:

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.