Removing items inside an array using for loop in lua - lua

I'm trying to use a for loop to destroy objects inside my array like so:
for item in self.objects do
item:removeSelf()
end
The self.objects is my array and it contains images I use for animation. If I touch one of these animated objects, it should be destroyed (disappear). My problem is, I'm getting this error message:
Attempt to call a table value
I'm not sure why I'm getting this error and how to fix it so can somebody please explain how I can remove objects from my array during touch event and why I'm getting this message? Thanks in advance. :D

A generic for loop of the form for x in y do... expects y to be an iterator function. You're passing it a table, hence the error message.
If you just want to iterate over each entry in the table, use pairs:
for key, item in pairs(self.objects) do
item:removeSelf()
end
See PiL 4.3.5 and all of Chapter 7 for more info on generic for and iterators.

Related

Adding Items from Lists to Dictionaries in Apple Shortcuts

I have this Shortcut which queries a Notion database, then fetches the IDs and titles of pages from the database and turns them into key : value pairs.
I want to add those key : value pairs to the dictionary shown at the top of the screenshot. The problem is, even though I can see that I'm adding to the dictionary inside the Repeat With Each Item loop -
When I check the contents of my Categories dictionary at the end of the Shortcut, those entries haven't been saved.
I have seen this answer, which appears to solve the same problem. But when I implement it and then check the contents of the variable inside the loop, nothing is even being set here.
I'd be very grateful for any pointers here.
I figured out the problem - I needed to set the dictionary as a variable outside the loop -
And then I can update the dictionary inside the loop -
I don't know why that works but it does ¯\(ツ)/¯

Array of object append not working on swift [duplicate]

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.

How to create an empty Results<T> object?

I'm trying to create a MutableProperty which holds a Results received from Realm.objects(_:).
To create the property I need to give it an initial value; hence an 'empty' Results.
I've tried creating one using:
var someThings = Results<SomeObject>()
MutableProperty(someThings)
But the compiler gives me the error: Cannot invoke initializer for type 'Results<SomeObject>' with no arguments.
While I understand the error, I'm not really sure how to create a Results object in this context.
Looking at the source of Results I couldn't find an init either.
So my question is; how can I create a Results myself to use in a MutableProperty?
Edit:
I've seen this question...but that doesn't really help (unless I'm going to create a "wrapper" for the MutableProperty or something).
With help of the comments on my OP; I created a mutable property with an empty set of results by fetching objects with an 'invalid' filter.
E.g. MutableProperty(realm.objects(SomeObject.self).filer("EMPTY SET")).

how to get data form NSMutablearray in to NSString

Hello.......
in my apps, i have problem with xml parising .i have same tags differentiate with integer value.each tag has different value.
now the problem me facing is accessing this value for that particular tag only.
plz anybody have idea abat this.
let me know.
mean's the problem is that i have one MutableArray and all record in it. and i want to show some values in one view and reaming some value show in another view and reaming in another view..
But the MutableArray is same..
and one more thing is that i want to get tag value
<subject>XYZ</subject>
the output is subject i want.
i do not need of XYZ i need only subject..
sorry i can not show what i want but please understand my question and give me the answer
Thanks
Use NSMutableDictionary instead NSMutableArray.
From documentation.
The NSMutableDictionary class declares the programmatic interface to
objects that manage mutable associations of keys and values. It adds
modification operations to the basic operations it inherits from
NSDictionary.

Ruby on Rails Array access

My issue involves an array that is apparently not nil but when I try to access it, it is.
The array is returned from a find on an active record. I have confirmed it is in fact an array with the .class method
#show_times = Showing.find_showtimes(params[:id])
#show_times.inspect =>shows that the array is not empty and is a multidimensional array.
#show_times[0] =>is nil
#show_times[1] =>is nil
#show_times.first.inspect => NoMethodError (undefined method `first')
I am perplexed as to why this is..
It's not an array, it's an active record magic structure that mostly looks like an array. I think you can do to_a on it to get a real array, but then a lot of the magic goes away.
find_showtimes is not a built-in finder - if it were, it would return either a single ActiveRecord object or an array of ActiveRecord objects - never a multidimensional array. So the first thing I'd do is have a look a the source of that method.
EDIT: Given your source code (in the comment) - you're actually getting a Hash, not an Array, back from your finder. See the docs for Enumerable's group_by. The Hash is mapping your theater objects to an array of showing objects. This is what you want to do:
Showing.find_showtimes(params[:id]).each do |theater, showtimes|
puts "Theater #{theater} is showing the movie at #{showtimes.join(', ')}"
end
If you're in the console and you want to explore the #show_times object, you can use the keys method to see all the hash keys and then get to individual hash items (such as all the show times) like so:
#show_times[:theater_one]
Same thing will work in your model/view but using each/map/collect would be cleaner as pretty code goes.

Resources