Array (class) filled with non nil values stays empty - arrays

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.

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.

Related

replacing the value of an element in an array without creating a new array

I'm learning Ruby and have been practicing by solving problems on Codewars and Leetcode. I've come across this problem in Leetcode where it is asking me to, given an array and a value, modify the array in place by removing an occurrence of the value given in the array. Pretty simple! I was able to solve it- but, this curious thing happened and I don't know why!
Here's my code:
def remove_element(nums, val)
nums.each_with_index do |num, index|
if num == val
nums[index] = nil
end
end
nums.compact!
nums.length
end
You can see here that on line 4 I've written "nums[index] = nil", and this worked just fine for me. However, for the longest time I was trying to solve the challenge by writing "num = nil". What doesn't make sense to me is, why does "nums[index]" work and not "num"? Don't they refer to the same thing?
Answer from Dave Newton:
num is a block-local variable, nums is the array. Modifying a local parameter is different than accessing a reference. As another example, say that the array was filled with objects. num.some_property = 5 would modify the property of the array entry, num = SomeNewObject.new would just create a new object and not modify the array entry. Same thing would happen if you were calling a function.

Dynamic Array of flash AS3

I have an array to store true answers and false answer of random frame multiple choices questions:
var arraytruefalseanswer=[];
I use push method to insert every true and false answer in the array:
arraytruefalseanswer.push(trueanswer)
arraytruefalseanswer.push(falseanswer)
The problem is:
I can not remove the last element of arraytruefalseanswer .
Because
If I use pop method arraytruefalseanswer.pop(),
it will remove all elements in the array arraytruefalseanswer or bring back to
arraytruefalseanswer=[]
If i use delete, it is still leaving null.
Please help... how can I remove the last element of arraytruefalseanswer using flash AS3?
Thank you.
The documentation says: When you do a pop(), it returns items that were popped, and the array gets modified as a side effect. Therefore, in order to just remove the last element, you call arraytruefalseanswer.pop(); as is. You can use trace(arraytruefalseanswer) to verify if anything popped. Also check your code flow, it's possible that when you're popping your last element, you think the code reaches the call once but it's not so, and it say pops your entire array so you've left with an empty array. I can't say more without ever seeing your entire block of code where you work eith your truefalseanswer.

How do I replace an array element in LabView? (2d array of pictures)

so I have a final project for a class where I need to make a video game in LabView. The issue I'm having at the moment is that I can't figure out the 'right' way to put 'yourShip.png' into the 2d array of 2d pictures at [0,0]. Every tutorial I can find basically has exactly what I have down below in the screenshot, and it makes sense to me. However, running the program quickly shows that it does nothing.
To describe the code, I have a path constant that leads to the picture, which feeds to a draw flattened pixelmap function. Up to this point I know the code works, since creating a test indicator reveals as such. However, next I try to use the replace array subset function to replace the (default blank) 2d picture at [0,0] with yourShip.png. 'screen' is a 5x5 2d array of 2d pictures. The local variable of the same name being outputted to is indeed the very same array.
My main guess with why my code doesn't work is because of the way I'm taking screen as the input variable and then outputting to it via a local variable. However, if this is wrong, I'm confused with how I should do it. All I want to do is 'spawn' the image at the correct index.
The replace array subset works quite literally, i.e. it can only replace existing elements.
If there is no element at the specified index because the array is smaller, the function will do just nothing.
I guess your array is empty, so, initialize your screen array first to a size of at least 1x1.

Why can't I use the Match method on an array in ruby?

I need to iterate and parse 108 lines from a file and then sort them into 3 different hashes. (In one iterator.) (In Ruby)
I have the file loaded into the program and into the array I need to parse. When I try to make the iterator anyway I try to use the Regex Match command I get an error abut the unknown method. Is as simple that I can't use that method on a array?
lines = File.readlines('access_log')
lines.each.match(/^([:\d\.]+) .*\[.*\].*\"[A-Z]+ *(.+) HTTP/)
This and every other way I have tried to use the match method it hasn't worked.
This also doesn't anything for the hash, as I haven't done that yet.
/^([:\d\.]+) .*\[.*\].*\"[A-Z]+ *(.+) HTTP/.match(lines)
Have also tried this, but the error output appears that you cant run it on only the array. I beilive this is where I would need to tie the iterator in, but I'm stumped.
So, what's happening is that what readlines does is it slurps the entire text file.
So you have an array with the content of the textfile separated by a newline(and the newline is kept in every string in the array).
After that, you're doing lines.each, which brings out an enumerator. Then you're calling .match on the enumerator instead of the string itself
The proper way to do this would be
lines.each { |line| line.match(/^([:\d\.]+) .*\[.*\].*\"[A-Z]+ *(.+) HTTP/) }
However, the above actually won't do anything because all you're doing is iterating against each element and checking if it matches the REGEX.If you want it to actually do something, try...
matches = lines.map { |line| line.match(/^([:\d\.]+) .*\[.*\].*\"[A-Z]+ *(.+) HTTP/) }
Remember that the match method only works on strings. If match matches something, it returns an object of the class MatchData, else if it doesn't match anything-- nil.

Array.Clear throwing out of bounds exception

I'm having trouble with a clear statement. I've got an array that gets sized dynamically, filled, and then passed to a function to be converted to some custom objects.
After that conversion I want to clear the array. I use
Array.Clear(FileData, 0, FileData.Length)
as this thread suggests (reset-an-array-to-default-in-visual-basic). However, every time I get to that point in the script the try-catch wrapping the Clear catches an out of bounds exception on the Clear.
The array is not empty (actually it's got ~34900 items) so it's not that the array has zero length. The one thing that it might be that isn't discussed in the question I referenced above is that my array is 2 dimensional.
All that said, I'm fairly well stumped. Any help would be appreciated.
UPDATE: For those experiencing this issue, I ultimately just left the step commented out, so that instead of clearing and then setting the array to Nothing, I just set it to Nothing. Doesn't really solve the underlying issue, but (should) free up memory all the same.

Resources