Iphone JSON-Framework - iphone

is posible using JSON-Framework for Iphone to know if a tag exists inside the JSON like in JAVA with the function hasTag(String)?

If by tag you mean name/key and you’re using SBJSON, use -objectForKey: and test if the return value is nil. For instance, if person is an NSDictionary instance returned by the JSON parser and it can optionally contain a nickname,
if ([person objectForKey:#"nickname"] != nil)
{
// `nickname' is available; do something with it
}

The way I think to do it is to convert your JSON Object to an NSDictionary and after that use the method -(NSArray *)allKeys, or -(NSArray *)allValues depending what you want.
It returns an array with all the keys (or values) inside the object. You can then compare the keys with the one you want to find.
Hope it helps you.

Related

Accessing array/set of objects with keys

I want to be able to add objects to an NSArray and access them with Keys. Currently the way im doing it is creating a seperate NSDictionary of key-value pairs where the value is an integer number representing the index in my NSArray. This seems like an extra step to me.
If my understanding of NSDictionary is correct, only 'values' can be stored: a pointer to an object cannot.
Surely there must be an equivalent NSDictionary type function that allows objects to be stored and accessed with a key? I have looked through the documentation, but cant seem to find any answers, unless im missing something obvious.
NSDictionary is to store key value pairs. if you are adding key value pair after you created the dictioanry, use NSMutableDictionary class . example,
[dictionaryObject setObject:#"" forKey:#"abc"];
You can store objects in NSDictionary and can be accessed via keys...
In short, no.
An array (NSArray) is an ordered collection of references to objects, so simply said, an ordered collection of objects.
As opposed to dictionaries, which are unordered and values are accessed by keys.
You understanding of collections is probably wrong, you don't store values, but pointers (references).
The extra step is necessary if you need to store the references in an array, but in this case, you should consider using a dictionary. An option is to use keys that take care of the order.
For example :
[myDictionary objectForKey:#"1"];
could be an equivalent of :
[myArray objectAtIndex:1];
Thats wrong, you can store objects in a NSDictionary. Look at the method dictionaryWithObjects:forKeys: or dictionaryWithObjectsAndKeys:
I have no experience in Cocoa but looking at the documentation it seems like NSDictionary (or at least NSMutableDictionary) should handle your request (without you using NSArray).
I think I understand your problem. My suggestion for you is to use NSMutableArray and macros, like:
NSMutableArray *array=[[NSMutableArray alloc]init];
#define SOME_MACRO 0
id someObject;
[array insertObject:someObject atIndex:SOME_MACRO];
id getterObject=[array objectAtIndex:SOME_MACRO];
Of course define the macros in the header file.

How to tell if object is in NSArray?

Is there a way to tell if a certain object is in an NSArray? The way I am adding objects to my array makes it possible for the same object to be added multiple times and I wanted to see if there was a way to see if it was already there (anywhere) in that array.
The NSArray containsObject: method is precisely for this purpose, its full signature being:
- (BOOL)containsObject:(id)anObject
See the full NSArray Class Reference docs for more information.
if([yourArray indexOfObject:yourObject] == NSNotFound) {
// your object is not in here
}
Edit: middaparkas approach is way better (if you don't want the index …)!

Dealing with <'null>' values in TouchJSON

I am deserializing some JSON using the TouchJSON framework. The array of dictionaries that comes from the parsing is used to populate a UITableView.
There is a chance that some of the values in the JSON I parse are empty. This results, if I NSLog it to the console, in the values looking like this:
id = 1234;
title = "Hello, World";
description = "<null>";
detail = "The world says hello";
Here the description value was an empty string when retrieved from the server.
So TouchJSON recognizes that the description values is of type string, but the original intention of the server was to communicate that this was an empty string, like description = #"";
If I later on try to set the value of description, to a UILabels text property the app will crash.
So my questions are, I have both NSNumbers and NSStrings in the JSON, should I traverse the result from TouchJSON's deserialize method and test all values and how would I do so?
I can't simulated what would happen if an NSNumber value was empty, how would I test for this? Will the NSNumber value be nil in that case instead of "null"?
I was using the SBJSON library and came up against the same problem. My solution would apply to your case too: I changed the library so that it handled missing values, setting them to +[NSNull null] in the collection it returned. That makes your client code a little warty, because you have to handle the cases where you might get an NSNull instead of an NSString. But this is just a more obvious version of the wart where you have to decide whether #"" meant an empty string or an unset value.

Is there a way to construct an object automatically from an NSDictionary?

I'm building an iPhone app that communicates with an external server via JSON. The JSON library I'm using parses the response string from the server into a dictionary. Currently I've got a method that I've written that just uses hardcoded strings as keys for the dictionary in a constructor I've written called initWithDictionary:(NSDictionary *)dic (e.g. self.name = [dic valueForKey:#"name"];. Is there some smart idiomatic Objective-C way to do this?
Just call [someObject setValuesForKeysWithDictionary:dictFromJSON] to set an object's properties from a dictionary.

Replacement for use of nil in dictionaries in objective-C

I'm working in the IPhone SDK, and am pretty new to objective-c. Right now I'm working with NSUserDefaults to save and restore setting on my IPhone app. In order to save the classes that have been created, I encode them into dictionary form, and then save the NSdictionary.
My problem is that I can't find a reasonable way to store a non-value (I.E. a class variable that is nil) in the dictionary in a reasonable way. To be more specific, lets say I have a class "Dog" and it's got NSString *tail-color. Lets say I'm trying to save a class instance of a dog without a tail, so tail-color for that instance is nil. What is a reasonable way of saving dog as a dictionary? It won't let me save nil into the NSdictionary. #"" isn't good, because if I do if(#""), #"" is true. I would like it to be false like nil.
I hope my question makes sense, and thanks for your help!
If you don't store anything for that key, nil will be returned when you call objectForKey:. If you check for nil when reading the data in, would that be enough? Optionally, you can use objectsForKeys:notFoundMarker: that will return a default value instead of nil.
So, store nothing at all in the dictionary for a value you don't have, and use a strategy for handling that value missing when reading.
You could use NSNull, but that doesn't feel standard, IMO.
You can use NSNull. Instantiate it like this:
[NSNull null]
I would recommend Archiving to save and restore your objects, however.
You should use NSNull to represent nil objects in collections
The best solution is to not save the values which are 'nil' in your case. While reading if the value is not present for your given key the dictionary will return you 'nil'.