NSInvalidArgumentException while adding NSDictionary to NSArray - iphone

I have declared NSDictionary *dict; globally and trying to add this dictionary into array but getting an Error :
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
My Code :
dict = #{#"Testcase" : Testcase, #"TestResult" : TestResult,#"Message": Message};
NSMutableArray * myArray = [[NSMutableArray alloc] init];
[myArray insertObject:dict atIndex:TestcaseId];
How to solve this Error ?

Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '* -[__NSArrayM
insertObject:atIndex:]: object cannot be nil'
Problem : From the above ErrorMessage, it is clear that you are trying to add NULL value in your NSDictionary.
Explanation : At the time of inserting Value to your NSDictionary , note that the Value shouldn't be NULL and Non-String. You are trying to add Value of Variable to your Dictionary directly. Instead you should try to add like this :
Answer :
[NSString stringWithFormat:#"%#",Testcase];
Benefits :
It will add String-Value.
It will add Empty-Value when your Value of Variable is NULL.

dict = [NSDictionary dictionaryWithObjectsAndKeys:Testcase:#"Testcase", TestResult: #"TestResult",Message, #"Message", nil];
NSMutableArray * myArray = [[NSMutableArray alloc] init];
[myArray insertObject:dict atIndex:TestcaseId];
Make sure TestcaseId is not more than your array count

[myArray addobject:dict];
or
[myArray replaceObjectAtIndex:TestcaseId withObject:dict]

Related

Separate string by comma , data coming from JSON

I am facing the following issue,
I have parsed data from a server and I need all keys to be put into the arrays.
(
"shopping|TD|Shopping|TD|customer/shopping_icon.png",
"salon_spa|TD|Salon & Spa|TD|customer/salon_icon.png",
)
These are the keys I'm getting from the server, now I want to put them into an Array.
I have tried using component separated by string but that always crashes the app.
NSMutableArray *allKeysArray =[[NSMutableArray alloc]init];
[allKeysArray addObject: [deals allKeys]];
NSLog(#" all keys --%#",allKeysArray);
NSMutableString *string=[[NSMutableString alloc]init];
string =[allKeysArray objectAtIndex:0];
NSLog(#"string--%#",string);
arr =[string componentsSeparatedByString:#","];
The app crashes saying component separated by string is Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[_]: unrecognized selector
sent to instance 0x75b8dd0'
Replace your code from ....
[allKeysArray addObject: [deals allKeys]];
as like this...
[allKeysArray addObjectsFromArray: [deals allKeys]];
This will solve your problem...

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

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.

Searching an NSMutableArray with a String and returning the entire compared Array

it seems my Problem isn't a problem for anybody eles because I havend fount anything about it.
so it maybe isn't such a big Problem but for me it is.
I have this MutableArray filled with alot of data from an XML file.
-Name -Age -Address
The search goes for the Name, and the filtering works pretty fine so far.
what I do is search the array with rangeOfString but that only returns the String (-Name) and not the Array with it's content like the original Array because its only a string now.
can anyone tell me how do I accomplish this
That's my search so far
if ([[self searcher] length] != 0)
{
for (NSString *currentString in [self listOfContent])
{
if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound)
{
[[self filteredListOfContent] addObject:currentString];
}
searcher is the String in the SearchBar.
or is there any other more efficent way or is it possible to search for any value in the MutipleArry?!?
Any Ideas and suggestions are welcome
I changed the code to this
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
for (NSDictionary *dictionary in listOfContent)
{
//NSArray *array = [dictionary objectForKey:LNAME];
[searchArray addObject:dictionary];
}
for (NSString *sTemp in searchArray)
{
NSLog(#"array %#", searchArray);
if ([sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch].location != NSNotFound)
[filteredListOfContent addObject:searchArray];
}
the log shows that the filter seems to work but then I get this error
2010-10-22 16:18:09.708 TableView[6114:207] -[__NSCFDictionary rangeOfString:options:]: unrecognized selector sent to instance 0x5c3f540
2010-10-22 16:18:09.712 TableView[6114:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary rangeOfString:options:]: unrecognized selector sent to instance 0x5c3f540'
can anyone tell me what the problem is
And Still no solution found I changed the Code to this:
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
for (NSDictionary *dictionary in contentsList)
{
NSArray *array = [dictionary allValues];
[searchArray addObjectsFromArray:array];
}
for (NSDictionary *dict in searchArray)
{
if ([[dict valueForKey:#"NAME"] rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound) {
NSLog(#"Filter %#", dict);
[searchResults addObject:dict];
}
now i Have the array with the values but still get the error
2010-10-28 16:23:46.124 TableViews[8373:207] *** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 0x5a5eb00'
can anyone explain me waht taht that error means or waht I did wrong?!?
2010-10-28 16:23:46.124 TableViews[8373:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 0x5a5eb00'
That error means you treated an NSString as if it were an NSDictionary by sending the message -objectForKey: to it.

Adding Object NSMutableArray with ForEach Loop

here is what i am trying to do:
NSMutableArray *objectNames = [[NSMutableArray alloc] init];
for (Object *o in objectList){
if (![objectNames containsObject:o.name]) {
[objectNames addObject:o.name];
}
}
I am trying to go through an array of objects, then take the objects name (a string) and add it to a string array of objectNames.
This code works in the simulator just fine. but when i run it on the device i get this error.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFArray insertObject:atIndex:]: attempt to insert nil'
One or more of the objects in objectList has it's name property set to nil. This leads to you trying to insert just nil into objectNames, which gives you the exception.
If it's OK for an object to have a name of nil, what you need to do is to check for this before you insert into objectNames:
NSMutableArray *objectNames = [[NSMutableArray alloc] init];
for (Object *o in objectList){
if (name && ![objectNames containsObject:o.name]) {
[objectNames addObject:o.name];
}
}
Looks like one of your Objects doesn't have an name set correctly