JSONKit invalid arguement when try to copy - iphone

I am parsing JSON data with JSONKit as NSMutableDictionary.
NSString *str = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];
NSMutableDictionary *jsonResponse = [self.responseData objectFromJSONData];
NSMutableDictionary *newData = [[NSMutableDictionary alloc] init];
[newData addEntriesFromDictionary:[jsonResponse mutableCopy]];
When i do this i am getting this error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMutableDictionary addEntriesFromDictionary:]: dictionary argument is not an NSDictionary'
I am trying to figure out what is causing this problem. I know that jsonResponse is an object of JKArray from my other experience.
I need help.
Thanks.

Try the following:
id object = [self.responseData objectFromJSONData];
NSLog(#"%#", [object class]);
Most likely your response is an array instead of a dictionary.
If you really want to convert the array into a dictionary, you could do something like this, using a self-defined key:
NSArray *array = [self.responseData objectFromJSONData];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObject:array forKey:#"posts"];
Though perhaps there are some better options if you could show me the contents of your array.

Related

how to update NSMutableDictionary

I have
NSMutableDictionary *mutDic;
its loaded with some values from other NSMutableDictionary
from an alert i am trying to update its value
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
[self.mutDic setValue:[[alertView textFieldAtIndex:0] text] forKey:#"lname"];
}
but i am getting this exception
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
how we can update dictionary ?
I had the same exception before even though my dictionary was mutable. Let me explain my scenario to you, may be it will help :
I had NSMutableArray of NSMutableDictionary,
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
dict = [array objectAtIndex:0];
[dict setObject:#"" forKey:#""]; <-- it was crashing on this line...
so I changed my code as below,
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:[array objectAtIndex:0]];
it worked fine :)
found out the exception problem, but not solved fully
on load i need to take values from other dictionary for that the method i used was wrong, i just assign oldDic to mutDic, i changed to
self.mutDic = [[[NSMutableDictionary alloc] initWithDictionary:manager.oldDic] retain];
their initialized it by
self.oldDic = [[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"F Name",#"fname",#"L Name",#"lname", nil ]retain];
that solved the exception

Selecting a single field in a fetch into an array to load a picker

trying to load a single field into an array and then loading that in a picker. I think its in the creation of the array that I have a problem.
This is my code:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescr];
[request setPropertiesToFetch:[NSArray arrayWithObject:#"name"]];
NSError *error;
NSArray *array = [managedObjectContext executeFetchRequest:request error:&error];
self.pickerData = array;
The error I get is this:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObject isEqualToString:]: unrecognized selector sent to instance 0x7bb8100'
Any help would be appreciated...
My guess is that you're assuming the objects in your pickerData array are NSString instances, when in fact they are NSManagedObject instances. In order to get the value of the name field, you should do the following:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSManagedObject *managedObject = [self.pickerData objectAtIndex:row];
NSString *name = [managedObject valueForKey:#"name"];
return name;
}
Please go through the documentation of NSManaged Object-
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObject_Class/Reference/NSManagedObject.html
Then fetch the data properly and pass to Picker.
Main issue is wrong kind of comparison of objects, due to which app is crashing.

NSMutableDictionary "unrecognized selector sent to instance"

-(void)saveDictionary:(int)counter
{
NSString *path=[[NSBundle mainBundle] pathForResource:#"Data" ofType:#"plist"];
NSString *test = [NSString stringWithFormat:#"%d", counter];
[theDictionary setObject:test forKey:#"Counter"]; <---- Error
[theDictionary writeToFile:path atomically:YES];
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self saveDictionary:[_viewController counter]];
}
Error:
-[NSCFString setObject:forKey:]: unrecognized selector sent to instance
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString setObject:forKey:]: unrecognized selector sent to instance
I can Load the value for Key "Counter" from plist.
If I want to save the new value for same key "Counter" ... Error.
Need Help, spent hours.
bye
Here is the Code to initialize theDictionary:
-(void)initDictionary {
if (theDictionary == nil) {
NSString *path=[[NSBundle mainBundle] pathForResource:#"Data" ofType:#"plist"];
theDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
theString = [theDictionary objectForKey:#"Counter"];
}
}
Found it!
theString = [[NSString alloc] initWithFormat:[theDictionary objectForKey:#"Counter"]];
Thanks All!
This is because the object stored in theDictionary is actually an NSString and NSString doesn't contain a method called -setObject:forKey. Check your code for any places where theDictionary is being assigned and be sure that is actually an NSMutableDictionary.
check the initilization .. you could have done it like this .
NSMutableDictionary *dictoForSyncing = [[NSMutableArray alloc] init];
Sounds like theDictionary is a string instead of an NSMutableDictionary. Where is it created and what happended to it in the meantime?
This says that 'theDictionary' is not a dictionary at all. Most likely it was released earlier and some NSString has taken its place.
Are you using ARC? Where was 'theDictionary' defined.
And have you tried the zombies Instrument to track this down? That should help.
It seems that your property theDictionary isn't actually a dictionary, but a string (NSString). Where is theDictionary defined?
NSMutableArray *myObjFromFile = ....;
NSMutableDictionary *tmpDictFromFile =
[[[myObjFromFile objectAtIndex:xx] mutableCopy]; mutableCopy];
[tmpDictFromFile setObject:"YOUR OBJECT"
forKey:"YOUR KEY"];

problem in parsing JSON response in iphone

i am getting json data from this link
now i want data from "html_instructions": part
NSDictionary *result = [stringtext JSONValue];
NSLog(#"here");
NSArray *resultarr = [result objectForKey:#"routes"];
NSString *string;
for(NSDictionary *di in resultarr){
NSLog(#"for loop");
string = [[di objectForKey:#"legs"] objectForKey:#"steps"];
}
but after printing "for loop" in console it is throwing an exception
2011-05-20 16:16:26.997 speedymap[759:207] -[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x62a7d60
2011-05-20 16:16:26.999 speedymap[759:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x62a7d60'
please help me
i want html_instructiuons field value
Check if resultarr and di really contains anything or not.
NSDictionary *result = [stringtext JSONValue];
NSLog(#"here");
NSArray *resultarr = [result objectForKey:#"routes"];
CFShow(resultarr);
NSString *string;
for(NSDictionary *di in resultarr){
CFShow(di);
NSLog(#"for loop");
NSArray *tempArr = [[di objectForKey:#"legs"] objectForKey:#"steps"];
}
http://jsonviewer.stack.hu/#http://maps.googleapis.com/maps/api/directions/json?origin=delhi&destination=noida&waypoints=&sensor=true
Check the viewer and set the values accordingly. [di objectForKey:#"legs"] returns an array. the first object of that array is a dictionary which has the key steps. But that too returns another array.

Getting error when trying to set value/key pairs with SBJsonWriter

Getting the following error when trying to set value/key pairs:
2011-06-21 16:21:16.727 agent[94408:207] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<SBJsonWriter 0xab31230> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key key.'
Here is the code:
SBJsonWriter *writer = [[SBJsonWriter alloc] init];
[writer setValue:#"val" forKey:#"key"];
NSString *json = [writer JSONRepresentation];
NSLog([NSString stringWithFormat:#"json: #%", json]);
that is using the key-value coding from the (NSObject) category,
to use the dictionary interface:
import JSON.h then:
NSMutableDictionary * dict = [NSMutableDictionary new];
[dict setValue:#"val" forKey:#"key"];
NSLog(#"json: %#", [dict JSONRepresentation]);
p.s. NSLog accepts a format, so you don't need to make a string from a format to pass to it.