Access one specific element of NSMutableDictionary getting null values in iOS - ios

I have an NSMutableDictionary that loads data from a certain source.
He is loading fine and while debugging i am sure he is loading and that in the end he has the exact number of elements. I also can see the elements as they should while debugging.
I just want to get the value of the the row having the key value = 3 .
I tried NSString * myString = [myMutabDict objectForKey:#"3"], considering that the value is in string ,and i followed it with the debugger, and i am sure that my NSDictionary has elements in it , and i can see them while debugging, and i can see the key 3 , but i still get null as an output …
What am i missing?

If you are sure you see a value with a key of 3 and you can't load it using the key #"3" then it is possible that the key is a number. Use:
NSString *myString = [myMutableDict objectForKey:#3];
or modern syntax:
NSString *myString = myMutableDict[#3];

Related

How to encode and decode the string properly?

I get the dictionary from web service and this dictionary contain encoded key-value pair. I used some code for decode this key-value.
Code no. 1 :
[NSString stringWithUTF8String:[[dict valueForKey:#"desc"] cStringUsingEncoding:[NSString defaultCStringEncoding]]];
But when I run this code then it get crashed and got the error [NSString stringWithUTF8String:]: NULL cString'
When debugging it, dictionary contain keys & values also "desc" key having a value.
On other side i have one code that worked fine.
Code no. 2:
[NSString stringWithFormat:#"%#",[dict valueForKey:#"desc"]];
But almost everywhere in my app, I used code no.1 and its worked very well.
and now i have totally confuse i this error [NSString stringWithUTF8String:]: NULL cString' came while i check dictionary contain keys & values.
Why do you create a C-String from NSString and then (back) NSString from C-String?
Why not simply
NSString *desc = dict[#"desc"];
And – as always – do not use valueForKey unless you can explain why you explicitly need KVC.

Objective-C Access Specific Item in Plist

I have created an app, and I am trying to access a URL that is stored in a plist file. At this state I am just trying to log the contents out. I am aware similar questions have been asked before, but I am asking specifically to my scenario how to I access Item 0. I am trying to access Item 0 inside InternalViaSafari this manifests itself inside the URLValidator and then that inside the Root. The code I have so far is:
NSString* filePath = [[NSBundle mainBundle] pathForResource:#"plist-file-name" ofType:#"plist"];
NSDictionary* plist = [NSDictionary dictionaryWithContentsOfFile:filePath];
NSString* name = [plist valueForKeyPath:#"URLValidator.InternalViaSafari"];
NSLog(name);
You cannot use keyPath like that for this as far as I know. InternalViaSafari is not a property of URLValidator dictionary. What's more InternalViaSafari is an Array not a String in your plist.
In order to get this string you'd need something like this :
NSArray *internalViaSafari = plist[#"URLValidator"][#"InternalViaSafari"];
NSString *name = internalViaSafari.firstObject;
What happens here, is that you get the value under the URLValidator key from your plist dictionary. This value is also a Dictionary (this can be clearly seen in the plist screenshot you shared), so you get the value under the InternalViaSafari key. This value is in turn an Array, which has Strings as its elements. In this example I extracted the first entry from this array.

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.

Cut one string into two others in Objective-C

I have an NSURLRequest being made that to a server that returns a string.
string = [[NSMutableString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
receivedData is the mutable array that the downloaded data is stored in. Everything works fine.
I have now, however, added another value to that string. An example of the returned string would be 14587728000000 , 376.99. Originally it was one value so I didn't have to do any splicing. But, now that I have another value, I want to be able to separate it into two different strings.
What should I do to separate the two values into different string? Some kind of search that goes till the first space, or something like that. I have access to the server, and the string is generated in PHP so the separator can be anything.
You can do this with the NSString componentsSeparatedByString method:
NSString *string = #"14587728000000,376.99";
NSArray *chunks = [string componentsSeparatedByString: #","];
You can find some other common NSString tricks (where I found this one) here.
Use -(NSArray *)componentsSeparatedByString: and pass in the token to split by.

Resources