Adding objects from array to view later on in NSTableView? - ios

I'm totally stumped here I have no idea whats going on below is the code I wrote to just go through the array and print out all the other array values within the property list:
for (userFoodDataArray in userFoodDataArray){
NSLog(#"%#", userFoodDataArray);
}
What I'm trying to do "I THINK" is add them as objects so later on I can view them through a search or maybe an NSTableView could someone please head me in the right direction for what I need to do.
Also when i use that NSLog statement the format of how the print out is odd see below:
2014-01-23 21:45:40.212 Carbies[4425:70b] (
Coconut
)
2014-01-23 21:45:40.212 Carbies[4425:70b] (
Watermelon
)
2014-01-23 21:45:40.212 Carbies[4425:70b] (
Egg
)
they have these weird brackets around them why?

This is not into the real object, just NSLog way to print the NSArray object collection.
When you get your object back into NSString or on any label this will be only "String" not any bracket.

It is considering each object as an array. so you can use [userFoodDataArray firstObject], it will return the first object. In your case the only onject it has.

Related

How do I set an array of arrays in NSUserDefaults?

I want to set an array of arrays with NSUserDefaults like this:
let array = [["Mexico","USA","Canada"],["Mexico City","Washington","Ottawa"]]
NSUserDefaults.standardUserDefaults().setObject(array, forKey:"Countries")
Then I try to access it
var recalledArray = NSUserDefaults.standardUserDefaults().objectForKey("Countries") as! NSArray
println(recalledArray)
But this is what the console prints:
(
"\U00bfMexico",
"\U00bfUSA",
"Canada"
)
I only get the first array, missing the second one:
["Mexico City","Washington","Ottawa"]
Why am I only getting back the first array?
How do I get back both the arrays?
Im so sorry there was another problem, I was checking if the app was first launched then save the NSUserDefaults if not then dont save it again so i added the second array after I runned the app on the simulator so the code was never saved to NSUserDefaults, but anyway thank you for trying to help

NSString from NSString to set as key in NSMutableDictionary

I am getting a initWithObjects:count:]: attempt to insert nil object from objects[0] at the following line:
[contentsOfCocktails setObject:[NSMutableArray arrayWithObject: recipeTitleA] forKey:cocktailsTitleA];
recipeTitleA is the string I'm creating from the cocktails.recipeID class property that equals A. However, I am getting a recipeTitleA equals nil in the debug window.
Here is where I set cocktails.recipeID equal to recipeTitleA:
if ([cocktails.recipeID isEqualToString:#"A"]){
recipeTitleA = cocktails.recipeID;
}
Is this the correct way to set a string equal to another string in order to use it as a key in a NSMutableDictionary?
Long story short: I am trying to extract the recipeIDs that equal A and set them as a key in a dictionary. I will be doing this with other letters as keys as well. I was then going to store them in an array in which I could create sections in the tableview. Data is brought in with FMDB.
I'm new to obj-c and new to data formatting with arrays and dictionaries. Any help would be greatly appreciated!
It looks like you are doing the string comparison correctly. Set a breakpoint at if ([cocktails.recipeID isEqualToString:#"A"]){ and see what you are getting for cocktails.recipeID, if it's nil go back into your cocktails.recipeID property and look if its being set correctly.

My NSDictionary somehow has multiple values for one key

I have been attempting to debug a issue with my code, and just came upon an odd phenomenon. Found this in the debug area when a breakpoint was triggered:
Am I correct in observing that there are multiple values for this key: #"6898173"??
What are possible causes of this? I do not set those key-value pairs using the string literal, but by getting a substring of a string retrieved and decoded from a GKSession transmission.
I still have this up in the debug area in xcode, incase theres anything else there that might help.
EDIT:
By request, here is the code that would have created one of the two strings (another was created at an earlier time):
[carForPeerID setObject:[[MultiScreenRacerCarView alloc] initWithImage:[UIImage imageNamed:#"simple-travel-car-top_view"] trackNumber:[[[NSString stringWithUTF8String:data.bytes] substringWithRange:range] intValue]] forKey:[[NSString stringWithUTF8String:[data bytes]] substringFromIndex:9]];
The string in data might look something like this:
car00.0146898173
EDIT:
Code that sends the data:
[self.currentSession sendData:[[NSString stringWithFormat:#"car%i%#%#", [(MultiScreenRacerCarView *)[carForPeerID objectForKey:peerID] trackNumber], speed, [(MultiScreenRacerCarView *)[carForPeerID objectForKey:peerID] owner]] dataUsingEncoding:NSUTF8StringEncoding] toPeers:#[(NSString *)[peersInOrder objectAtIndex:(self.myOrderNumber + 1)]] withDataMode:GKSendDataReliable error:nil];
Sorry its hard to read. Its only one line.
What you're seeing is a debugger "feechure". When you have a mutable dictionary and modify it, the debugger may not show you the correct view of the object.
To reliably display the contents of an NSMutableArray or NSMutableDictionary, switch to the console and type po carForPeerID.

NSNumber in NSDictionary is always null

I'm building a project and want to add a NSNumber into a NSDictionary. But the it crashed because of the null value. So, I created another small program to check what happened. As you can see in the snapshot: Why the the value of NSNumber in NSDictionary is null?
I've run your code and I could reproduce the problem. But it seems like a debugger problem. For instance, if after your dictionary is created, go to the console and try printing the dictionary.
po dictionary
My result is like:
$4 = 0x2083e770 {
number = 1;
}
So it's not null at all. Also, after that, anum is assigned correctly and b is set to YES. So it really looks like a debugger issue instead of a bug from you.

If Statement Failing sometimes with id BoolValue Comparison?

I am pulling data from the web that is formatted in JSON and when I parse the data using "ValueForKeyPath" it stores the string value as an id object.
So I store the data into a NSMutableArray
In the debugger window it shows all the elements added as (id) null.
I have an if statement
if ([[self.activeCategories objectAtIndex:selected] boolValue] == true)
Sometimes I would say 20% of the time it fails the if statement when it should not.
I was wondering if it was because the self.activeCategories is storing id types. Do I need to do [NSString stringWithFormat#"%#", x] to all the objects inside the array? It seems like when I just straight cast it by using (NSString *) it is still type id in the debugger.
It's a very strange error to me... as the error is not consistently reproducible.
Try it like that:
if ([[self.activeCategories objectAtIndex:selected] boolValue])
According to that article a BOOL may hold values other than 0 and 1 which may fail the comparison.

Resources