Retrieve text from array - ios

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!

Related

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.

Accessing object from array within an array (defined in model class)

I suspect the solution to what I'm trying to do is fairly straight forward--yet, I'm unable to get it working myself.
Here's what I'm trying to do: I've built a card game that is initialized with the cards when the game loads. Now, some of those cards have certain properties (card type, card name, as well as a special array). The card type and card name objects are fairly easy to retrieve since they're just one object (and I can call using objectatindex). However, the special array contains several keywords that fluctuate depending on which card is chosen. So instead of initializing these keywords one by one (like I did for card type and card name), I put them into their own special array.. or an array within an array. Here's my code:
itemObjects class:
#synthesize cardName=_cardName;
#synthesize cardType=_cardType;
-(id) initWithCardName:(NSString*)cardName initWithCardType:(NSString*)cardType initWithSpecialArray:(NSArray*) specialArray{
self=[super init];
if (self){
_cardName=cardName;
_cardType=cardType;
}
return self;
}
model class
-(NSMutableArray*)deck{
if (_deck==nil){
_deck=[[NSMutableArray alloc]initWithObjects:
[[itemObjects alloc]initWithCardName:#"The Long Way" initWithCardType:#"bill" initWithSpecialArray:[[NSArray alloc]initWithObjects:#"fast", #"high", nil]],
[[itemObjects alloc]initWithCardName:#"A Short Cut" initWithCardType:#"bill" initWithTrendArray:[[NSArray alloc]initWithObjects:#"small", #"tall", nil]],nil];
View Controller class (this is where I'm trying to call one of the objects, "fast" for example, but with no success
NSString* testing=[[[self.model.deck objectAtIndex:indexPath.row]arrayForKey:#"specialArray"]objectAtIndex:0];
NSLog(#"%#",testing);
I believe I've initialized my "specialArray" correctly and the issue is with how I'm attempting to call it but if I've made a mistake there, any advice would be much appreciated. Thanks!
EDIT: This particular issue has been solved thanks to WendiKidd. It turned out that I wasn't initializing my specialArray correctly. This has led to a separate issue which I have linked to here. I've also posted my corrected code below for those interested in the future:
#synthesize cardName=_cardName;
#synthesize cardType=_cardType;
#synthesize specialArray=_specialArray;
-(id) initWithCardName:(NSString*)cardName initWithCardType:(NSString*)cardType initWithSpecialArray:(NSArray*) specialArray{
self=[super init];
if (self){
_cardName=cardName;
_cardType=cardType;
_specialArray=specialArray;
}
return self;
}
You aren't initializing your special array at all. You pass it to initWithCardName, but you're never setting it to anything. You need to store the special array inside that class, just like you do with cardName and cardType.
Secondly, I can't make heads or tails of the line where you're trying to access the special array. You haven't given us the proper information to see where the object is being stored to tell if you're even properly accessing a card object, but I definitely don't see where you've ever used the key #"specialArray" before, so there's no reason to expect that will return anything. However the rest of your data structure works, at some point you're going to have an object of whatever class the initWithCardName function initializes (for example purposes I'm going to go ahead and call it CardObject). To get the info from the special array, you're going to have to save an object called specialArray into the CardObject class, as already mentioned. Then you can write something like this:
CardObject* card = //[whatever you have to do to access the right card object]
for(NSString* specialAttribute in card.specialArray)
{
NSLog(#"Special Attribute: %#", specialAttribute);
}
And that should print all the special attributes of the card quite nicely.
If you really do just want the first item in the list, as your example was trying to access, this should work just fine:
CardObject* card = //[whatever you have to do to access the right card object]
NSLog(#"Special Attribute: %#", [card.specialArray objectAtIndex:0]);

NSArray containsObject method inconsistent results

I have an NSMutableArray called selectedUsers to which I am adding objects using a method called addUser. The objects being added are most often of type PLManagedUser (a core data managed object) although sometimes the object could be a string. In any case, if the array already contains the object, I do not want to add it to the array. Here is the code:
- (void)addUser:(id)user withTitle:(NSString *)title {
if (![_selectedUsers containsObject:user]) {
[_selectedUsers addObject:user];
}
}
I have noticed that if I try to add the same user back to back using the above method, the containsObject catches it, and duplicates are not added. However, if I add the same user (with the same memory address) after having added other objects in between, the duplicate will be added.
I am printing the contents of the array each time I add something to confirm that the duplicate objects are in the array.
My question is, is there any obvious reason why containsObject isn't consistently working here?
You need to provide the ability for an object to identify itself as equal to another object of the same class, and to do this you implement the isEqual: and hash methods.
Having said that, the explanation in your question is the opposite of what I would have expected.

Deleting the proper CCSprite from an array in a schedule

Before I explain my issue, I made this very not neat illustration of how my project works -
My issue is - when the time that I want to check damage is up, I want to delete that object from the array! I have tried deleting it in the checkForDamage, but as this gets called with ccTimeit just deletes every object (when I use removeObjectAtIndex:0 to get rid of the first one). I can't put it in the stopCheckDamagebecause while the damage on the first one is being checked, the player might as well have put another bomb down.
The checkForDamageworks just fine when the user gets hit, I break; it and calls stopCheckDamage. My problem is when the user does NOT get hit, because then the nonexisting sprite stays in the array and just messes stuff up. I have been thinking about every way I know of and I can't seem to find a way to delete a specific object after three seconds delay if the player doesn't get hit.
I also made a pastebin for the relevant code, which you can find here
This is just an idea,
You have an array of all of the objects. You just need to know which one to delete.
So, why not give each object a tag that gets added into the array. When you go to delete that object test its tag and delete it.
//Say your array has 10 objects in it,
//There will be 10 objects each with a tag 1-10.
//When you want to delete an object,
EDIT
//Before you add each object to the array, use a `for` loop
for (int i = 0; i < theMaxNumberOfTagsYouWant; i++)
{
self.myObject.tag = i;
[self.myArray addObject:self.myObject];
//This will loop thru as many times as you want, specify using the
//maxNumberOfTagsYouWant variable. and it will give it a tag, which'll be the value of `i`
//which gets increased for each object you insert into the array, Then when you want to
//remove the object, use the below code to remove the object using it's tag.
}
-(void)deleteObjectFromArray{
[self.myArray removeObjectAtIndex:myObject.tag];
}
Hope this helps a bit. :)

Display dynamic data in scrollable tableview in Cocos2d

I am making an app in cocos2d. I am using SWTableView library for displaying dynamic contents in tableview in cocos2d. There is absolutely no problem when I am running sample code from SWTableView Example
But when I load my data from server which is dynamic the tableview crashes on scroll.
It allows me to scroll well in the static data just like in the example but I am not able to figure out error as it just gives me BAD_ACCESS.
Note: Possible reason i feel is dynamic data.
If any better method is there to load dynamic data in scrollable way in cocos2d then please suggest.
Thank u.
I just found the solution. There was very strange problem but all working now.
Basically in the sample code (i mentioned in my question) there is a delegate method called
-(SWTableViewCell *)table:(SWTableView *)table cellAtIndex:(NSUInteger)idx;
here idx is NSUInteger but whenever i directly used it without converting it to int or NSInteger it gave BAD_ACCESS command.
Then i stored my data in MutableDictionary and gave it key as its index string
i.e for name data of first person i gave key as "0" (string) and value as name.
Hence whenever i retrieved those values i used
[name objectForKey:[NSString stringWithFormat:#"%d", idx]]
which worked absolutely fine.
Also to mention here the other delegate called
-(NSUInteger)numberOfCellsInTableView:(SWTableView *)table;
returns an integer value, but when i used [Values count] my app used to crash on scroll.
Values is my main array where i store my data received from server.
Finally i started storing my data count in NSUserdefaults and my problem got solved.
Hope this helps somebody.

Resources