I have this code:
NSDictionary *jsonGuardado = [self dictionaryFromJson:JSON];
NSLog(#"HOLA %#",jsonGuardado);
NSMutableArray *arrayCodigos = [jsonGuardado objectForKey:#"code"];
codigos = arrayCodigos;
(Codigos is an NSMutableArray)
The NSLOG return this:
HOLA (
{
code = 22051310;
},
{
code = 22051311;
},
{
code = 22051312;
},
{
code = 22051313;
}
)
But right after this, a message error says:
-[JKArray objectForKey:]: unrecognized selector sent to instance
I search this error in Google and here and all the questions I found not help me.
I'm using the JSONKit for make the dictionaryFromJson method.
You try this,
NSArray *jsonGuardado = [self dictionaryFromJson:JSON];
NSLog(#"HOLA %#",jsonGuardado);
NSMutableArray *arrayCodigos = [NSMutableArray arrayWithArray:[jsonGuardado valueForKey:#"code"]];
NSLog(#"arrayCodigos %#", arrayCodigos);
It will print only values.
The parser returns an array and not a dictionary in your case.
[self dictionaryFromJson:JSON] returns a Json array but you are capturing it in NSDictionary
Your log says that it's already NSArray. You should try below
NSMutableArray *jsonGuardado = [NSMutableArray array];
jsonGuardado = (id)[self dictionaryFromJson:JSON];
NSLog(#"HOLA %#",jsonGuardado);
NSMutableArray *arrayCodigos = [[jsonGuardado objectAtIndex:0] valueForKey:#"code"];
codigos = [arrayCodigos copy];
NSLog(#"Array %#",codigos);
Related
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[jsonArray removeAllObjects];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
responseData = nil;
NSMutableArray *sdf = [(NSDictionary*)[responseString JSONValue] objectForKey:#"DataTable"];
NSMutableArray * myArray = [[NSMutableArray alloc] init];
NSMutableDictionary * myDict = [[NSMutableDictionary alloc] init];
if (([(NSString*)sdf isEqual: [NSNull null]])) {
// Showing AlertView Here
}else {
for (int i=0; i<[sdf count]; i++) {
myDict=[sdf objectAtIndex:i];
[myArray addObject:[myDict objectForKey:#"RxnCustomerProfile"]];
}
jsonArray=[myArray mutableCopy];
NSMutableDictionary *dict=[jsonArray objectAtIndex:0];
if ([dict count]>1) {
// Showing AlertView Here
}
}
}
Hi Everyone, I have an issue regarding the -[__NSArrayM objectForKey:]: .
Tried to solve but did not get the better solution for it. Please help me to
find the solution. Thanks In Advance
Below is the issues
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x19731d40'
This is a debugging problem and nobody can really solve it for you as you are using non-local variables whose definition and values are unknown, don't mention that you are using SBJSON (I guess), etc. But let's see if we can give you some pointers. Your error:
[__NSArrayM objectForKey:]: unrecognized selector sent to instance
That tells you that you sent a dictionary method (objectForKey) to an array (__NSArrayM). So somewhere you have an array when you think you have a dictionary.
Now you declare and allocate a dictionary:
NSMutableDictionary * myDict = [[NSMutableDictionary alloc] init];
but then assign to it:
myDict=[sdf objectAtIndex:i];
So this discards the dictionary you allocated and instead assigns whatever is at index i in the array sdf. How do you know, as opposed to think, that the element of the array is a dictionary? You don't test to check...
So where did sdf come from? This line:
NSMutableArray *sdf = [(NSDictionary*)[responseString JSONValue] objectForKey:#"DataTable"];
So that calls JSONValue on some unknown string, assumes the result is a dictionary (could it be an array? or a failure?), looks up a key (did your error come from this line?), and assumes the result is an array.
So what you need to do is go and test all those assumptions, and somewhere you'll find an array where you think you have a dictionary.
Happy hunting!
YOU FETCH THE VALUE IN ARRAY FORMAT AND YOU INTEGRATE METHOD IN DICTIONARY.
You do not need to iterate keys and values of dict can directly pass values to array inside else part like:
myArray = [sdf objectForKey:#"RxnCustomerProfile"];
Key RxnCustomerProfile itself containing array not dictionary.
Change your if else part use below code:
if (([(NSString*)sdf isEqual: [NSNull null]])) {
// Showing AlertView Here
}else {
myArray = [sdf objectForKey:#"RxnCustomerProfile"];
}
NSMutableArray *sdf = [(NSDictionary*)[responseString JSONValue] objectForKey:#"DataTable"];
Check Sdf
if([sdf isKindOfClass:[NSDictionary class]])
{
NSLog(#"Dictionary");
}
else if([sdf isKindOfClass:[NSArray class]])
{
NSLog(#"NSArray");
}
else if([sdf isKindOfClass:[NSMutableArray class]])
{
NSLog(#"NSMutableArray");
}
First of all it seems like your json is not actually correctly formatted. Without knowing what responseData looks like it's difficult to say exactly what is wrong. But in your code there are a few areas where it can be improved.
First of all you don't need to use [responseString JSONValue]. You can short circuit it entirely with
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
NSArray *sdf = responseDictionary[#"DataTable"];
Now, the rest all depends on the data in responseData.
But you can make your code a little bit cleaner with (if I understand what you're trying to achieve correctly:
NSMutableArray *myArray = [NSMutableArray array];
if ([sdf isEqual:[NSNull null]]) {
// Showing AlertView here
} else {
for (NSDictionary *myDict in sdf) {
[myArray addObject:dict[#"RxnCustomerProfile"]];
}
}
// No idea what you're trying to achieve here, but here goes:
jsonArray = [myArray mutableCopy];
NSDictionary *dict = jsonArray.first;
if (dict.count > 1) {
// Showing AlertView here
}
Some things to note. You make very liberal use of NSMutableArray and NSMutableDictionary for no apparent reason. Only use mutable if you're actually changing the array or dictionary.
I have the following situation where
I have an NSMutableArray filled with an xml file which I want to search.
When I enter something in the search field I get this Error:
-[NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x5b388b0
What does it means and how can I fix it??
I suppose the error is somewhere around here.
- (void)searchTableView{
searchedList = [[NSMutableArray alloc] init];
NSLog(#"new list %#", searchedList);
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
for (NSDictionary *dictionary in list) {
NSArray *array = [dictionary objectForKey:#"TITLE"];
[searchArray addObjectsFromArray:array];
}
for (NSString *TempArray in searchArray) {
NSRange titleResults = [TempArray rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResults.length > 0)
[searchedList addObject:TempArray];
}
[searchArray release];
searchArray = nil;
}
it means you are calling a method designed for an NSArray (countByEnumeratingWithState:objects:count on a NSString.
I don't know ifthis code is copy/paste from yours, but if so, at the end where you use [searchList addObject:TempArray] you don't have an object named searchList.
Also, work on your naming conventions. big time.
I have an app that I need to support till the original developer comes back.
Basically it has a method that groups the data into sections of UITableView.
It all works very nicely and shows the Sections by DogLocation.
This is the code I have :
-(void)loadSortedData:(NSString*)breedToLoad fieldToSort:(NSString*)sortField
{
self.items = [DbUtilities getDogsByBreed:breedToLoad SortField:sortField];
NSMutableDictionary * theDictionary = [NSMutableDictionary dictionary];
for ( Dog * object in self.items )
{
NSMutableArray * theMutableArray = [theDictionary objectForKey:object.DogLocation];
if ( theMutableArray == nil )
{
theMutableArray = [NSMutableArray array];
[theDictionary setObject:theMutableArray forKey:object.DogLocation];
}
[theMutableArray addObject:object];
}
self.sortedMediaItems = [[theDictionary allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
/* Save `theDictionary` in an instance variable */
self.theSource = theDictionary;
[self.tableView reloadData];
}
I call this method in my ViewDidLoad by using following syntax:
[self loadSortedData:#"Cattle" fieldToSort:#"DogLocation"];
What I am trying to achieve is to be able to call this method by passing a value in the method e.g so that I can sort it by other fields and not just the DogLocation e.g DogAge,DogPrice .
Could anyone kindly direct me or show me a better way to achieve this instead of writing big if else statement?
Thank you in advance.
You can try using the M13OrderedDictionary, it's based on NSObject not NSDictionary but it does the job required: https://github.com/Marxon13/M13OrderedDictionary
As long as sortField is always a valid property of Dog, you can use Key-Value Coding.
for ( Dog * object in self.items )
{
id key = [object valueForKey:sortField];
NSMutableArray * theMutableArray = [theDictionary objectForKey:key];
if ( theMutableArray == nil )
{
theMutableArray = [NSMutableArray array];
[theDictionary setObject:theMutableArray forKey:key];
}
[theMutableArray addObject:object];
}
Hello guys I am new to Xcode/iOS developing
I trying to add json data to the mutable array , and it results in app crash :(
so far here is my code:
if(! [defaults objectForKey:#"Person1"])
[defaults setObject:[PersonsFromSearch objectAtIndex:index] forKey:#"Person1"];
else
{
NSMutableArray *Array = [[NSMutableArray alloc]init];
id object = [defaults objectForKey:#"Person1"];
Array = [object isKindOfClass:[NSArray class]] ? object : #[object];
[Array addObject:[PersonsFromSearch objectAtIndex:index]];//crash here :((
[Array moveObjectFromIndex:[Array count] toIndex:0];
}
Crash Dump:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0xcb4d380'
* First throw call stack:
what is wrong here ? can you please help me to resolve this issue
Array contains this (Json?)
{
Address = "\U05d3\U05e8\U05da \U05d4\U05e9\U05dc\U05d5\U05dd 53";
CellPhone = "052-3275381";
EMail = "editor#pc.co.il";
EnglishPerson = "Yehuda Konfortes";
FaceBookLink = "";
Fax1 = "03-7330703";
Fax2 = "";
FileNAme = "100050.jpg";
HomeEMail = "";
HomeFax = "";
HomePhone1 = "";
HomePhone2 = "";
PersonID = 100050;
PersonName = "\U05d9\U05d4\U05d5\U05d3\U05d4 \U05e7\U05d5\U05e0\U05e4\U05d5\U05e8\U05d8\U05e1";
Phone1 = "03-7330733";
Phone2 = "";
ZipCode = "";
}
[defaults objectForKey:#"Person1"]; returns dictionary but not array.
So you can't use addObject method.
UPD
You may resolve this crash by creation array with a single object.
Here is updated code:
NSMutableArray *Array = [[NSMutableArray alloc]init];
id object = [defaults objectForKey:#"Person1"];
Array = [object isKindOfClass:[NSArray class]] ? [object mutableCopy] : [#[object] mutableCopy];
[Array addObject:[PersonsFromSearch objectAtIndex:index]];//crash here :((
[Array moveObjectFromIndex:[Array count] toIndex:0];
NSMutableArray *_arr = [[NSMutableArray alloc]init];
NSDictionary *results = PASS JSON Value;
[_arr add object:[results objectforkey:#“Address”]];
[_arr add object:[results objectforkey:#“CellPhone”]];
I'm trying to add an dictionary to an array to create an array on dictionaries, but when I test the app, the array is still empty. Take a look on my code:
NSMutableArray *listaEstabelecimentosPorCategoria;
for (NSDictionary *tempDict in listaEstabelecimentos) {
if ([[tempDict objectForKey:#"category"] integerValue] == 1) {
[listaEstabelecimentosPorCategoria addObject:tempDict];
NSLog(#"TEST");
}
}
NSLog(#"%#", listaEstabelecimentosPorCategoria);
The app prints this:
2013-08-18 12:16:38.802 Petrópolis[10157:c07] TEST
2013-08-18 12:16:38.802 Petrópolis[10157:c07] TEST
2013-08-18 12:16:38.803 Petrópolis[10157:c07] (null)
when I test the app, the array is still empty.
This is because you did not initialize listaEstabelecimentosPorCategoria:
NSMutableArray *listaEstabelecimentosPorCategoria = [NSMutableArray array];
NSMutableArray *listaEstabelecimentosPorCategoria=[[NSMutableArray alloc] init]; //initialize
for (NSDictionary *tempDict in listaEstabelecimentos) {
if ([[tempDict objectForKey:#"category"] integerValue] == 1) {
[listaEstabelecimentosPorCategoria addObject:tempDict];
NSLog(#"TEST");
}
}
NSLog(#"%#", listaEstabelecimentosPorCategoria);
NSMutableArray *listaEstabelecimentosPorCategoria this code hasn't been allocated to memory so it will be null value
NSMutableArray *listaEstabelecimentosPorCategoria = [NSMutableArray array];
then listaEstabelecimentosPorCategoria add the object.