Array of object append not working on swift [duplicate] - ios

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

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 ¯\(ツ)/¯

updates to global object not reflecting by changing value of local object in iOS

Today I faced a very strange issue in Objective C, if anybody can help me figure out, i would really appreciate it. Let me explain the scenario.
Below is the case:
I make a webservice call and get NSArray of Objects and cached in Global class.
I have a local NSArray in view controller which is nothing but assigned from Global class array. Like this
self.listOfObjects = [GlobalCache sharedInstance].listOfObjects
self.listOfObjects is strong and nonatomic.
In a scenario I updated an object in self.listofobjects, lets say nameProperty of the object in array. Its updated in self.listOfObjects and when i print object using command
po ((ObjectType*)self.listOfObjects[0]).name
its fine and printing the updated value.
But when i see the value in GlobalCache of the same objects, Its not reflecting sometimes its still having old values.
self.listOfObjects is just reference to the GlobalCache listOfobject. (No where it is copied from global cache to local object)
Could it be a memory problem?

Retrieve text from array

I am working on an app where I send a query to a webpage and get the information and save it into my array. Now, all this works except for then I come to take the value from my array and display it in my text view. using this code
-(void)viewDidAppear:(BOOL)animated
{
self.tvSoilN.text = [[_soilNotes objectAtIndex:0] objectForKey:#"crop"];
}
However, I get the error of the array being empty.
Now that makes sense because using a breakpoint I can see that the code above actually runs before the code be in my model.m file which I use to write the data in the array
I cant post images but when I use a break point on the if(self.delegate) line It shows me
_locations (and when i drill down i see)
_crop_soil(this is the field in my database)=#"Blah blah blah")
which makes me realize that my data is indeed being retreived from online. But i am just having difficulty in displaying it from my array or anywhere.
Would like some help in especially where to call my array as I also think i am doing it wrong.Thanks you
IN Implementation file
-(void)itemsDownloaded:(NSArray *)items
{
// This delegate method will get called when the items are finished downloading
// Set the downloaded items to the array
_soilN = items;
NSLog(#"soil%#",_soilNotes);
self.tvSoilN.text=[[_soil objectAtIndex:0]objectForKey:#"crop"];
}
Use valueForKey not objectForKey. Got there in the end!

NSMutableDictionary contents inconsistent with output of allValues

So long story short, there's a discrepancy between the output of a NSMutableDictionary's contents and the result of calling allValues on the same object. Below is some debugger output after inspecting the object which demonstrates my problem: (made generic of course)
(lldb) po self.someDict.allKeys
<__NSArrayI 0xa5a2e00>(
<SomeObject: 0xa5a2dc0>,
<SomeObject: 0xa5a2de0>
)
(lldb) po self.someDict.allValues
<__NSArrayI 0xa895ca0>(
0.5,
0.5
)
(lldb) po self.someDict
{
"<SomeObject: 0xa5a2dc0>" = (null);
"<SomeObject: 0xa5a2de0>" = (null);
}
So as we can see, the actual output of the NSMutableDictionary contains null values for both its entries, but the contents of .allValues contains the proper data. These three outputs were taken at the same time in execution.
I'm not sure why this is happening, but I think it may have something to do with the fact that I'm encoding/decoding the object which this dictionary is a property of using CoreData. I believe I'm doing this properly:
[aCoder encodeObject:self.someDict forKey:#"someDict"];
and to decode
self.someDict = [aDecoder decodeObjectForKey:#"someDict"];
The weird thing is that if I inspect the dictionary before it ever gets encoded, it is still in the state described at the beginning of the post, so this is why I'm doubtful the CoreData actions are screwing with the contents.
As always, please don't hesitate to request additional information.
EDIT: The problem was as answered below. I was using a custom class which didn't cooperate with isEqual, so my solution was to change the storage and structure of my logic, which made using a Dictionary unnecessary.
I have not been able to duplicate the problem using NSString as keys and NSNumber as values. I suspect that your custom class does not properly implement hash and/or isEqual. Specifically, the results from those two methods must be consistent, meaning that if isEqual returns true, then the hash values for the two objects must be identical.
You also need to ensure that your class implements NSCopying properly and that a copy is equal to the original.
As a general rule, don't use custom objects for dictionary keys. Just use strings and be done with it.
As user3386109 points out, custom objects must properly implement the -hash and -isEqual methods in order to be used as dictionary keys, and even then, custom objects don't work correctly for dictionary keys for things like key/value coding.

Removing items inside an array using for loop in 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.

Resources