NSString from NSString to set as key in NSMutableDictionary - ios

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.

Related

iOS: syntax to retrieve value of attribute in array?

I have an array of user objects, one of which is stored in a property _selectedUser. When I log it out, it displays as :
{
uid = 5;
uname = xxxx;
}
However, when I try to access the uid with the following, I get an error:
NSNumber *useridnum = _selectedUser.uid;
Error:
[NSKnownKeysDictionary1 uid]: unrecognized selector sent to instance
What is the proper syntax to retrieve value for uid?
You are trying to access the key as a property on the dictionary, not as a value it contains.
As D.C. suggested in the comments, use this code:
NSNumber *useridnum = _selectedUser[#"uid];
Or:
NSNumber *useridnum = [_selectedUser objectForKey:#"uid"];
You said in the comments that the object is an NSManagedObject. I'm not familiar with Core Data, but it looks like you need to use valueForKey::
NSNumber *useridnum = [_selectedUser valueForKey:#"uid"];
An NSDictionary does not store the keys directly as properties; it stores them in some kind of private data structure. It wouldn't be possible to dynamically store keys as properties without messing with the runtime, and then only a very limited set of strings could be used as keys (instead of basically every possible object), because the keys would have to be C identifiers.
You get this error because when you tried to access the key as a property, the system sent a message to the object saying to call the getter method for the property, but that getter method didn't exist, so the app crashed.

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

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