NSMutableDictionary issues - ios

I'm trying to insert an object in my dictionary, but first, I want to test if it's a NSMutableDictionary :
if ([[[self.response objectForKey:#"X"] objectAtIndex:0] isKindOfClass:[NSMutableDictionary class]]) {
[[[self.response objectForKey:#"X"] objectAtIndex:0]setObject:#"Hello" forKey:#"Y"];
}
I get this issue :
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'

You can check for the same selector which is crashing,
if([object respondsToSelector:#selector(setObject:forKey:)]) {
//If YES, it is NSMutableDictionary
}
else {
//Not mutable
}
Hope that helps!

Use isMemberOfClass instead of isKindOfClass

Try this,
if ([[[self.response objectForKey:#"X"] objectAtIndex:0] isKindOfClass:[NSDictionary class]]) {
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:[[self.response objectForKey:#"X"] objectAtIndex:0]];
[dict setObject:#"Hello" forKey:#"Y"];
NSMutableArray *xArray = [NSMutableArray arrayWithArray:[self.response objectForKey:#"X"]];
[xArray replaceObjectAtIndex:0 withObject:dict];
[self.response setObject:xArray forKey:#"X"];
}

Related

-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x9d0d720

- (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.

NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x165d5150'

Hi I am getting this data form server
NSDictionary*feed=[saveDic objectForKey:#"feed"];
NSLog(#"%#",feed); //Outputs: feed = ( { code = yQ7j0t; "user_id" = 889445341091863; } ); }
NSLog(#"%#",[feed valueForKey:#"code"]);
NSString *referralCode = [feed valueForKey:#"code"];
NSLog(#"%#",referralCode);
self.referralCode.text=referralCode;
And beacuse of that I am getting below error.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: selector sent to instance 0x165d5150'``
Any help will be appreciated.
The issue is, your feed key holds an array. You are not properly handling that in your code, that is why the crash occurs. When you call valueForKey: it retrieves an array of values held by that specific key.
For fixing that you can use:
NSArray *feed = [saveDic objectForKey:#"feed"];
NSArray *referralCodes = [feed valueForKey:#"code"];
NSString *referralCode = referralCodes.count ? referralCodes[0] : #"";
NSLog(#"%#",referralCode);
But I personally prefer using objectForKey: instead of valueForKey:. So you can re-write the code like:
NSArray *feed = [saveDic objectForKey:#"feed"];
NSString *referralCode = feed.count ? [feed[0] objectForKey:#"code"] : #"";
NSLog(#"%#",referralCode);
Some where you use a variable;
yourVaribleName.length
or
[yourVaribleName length]
which should be
yourVaribleName.count
note: the crash says exactly that "yourVaribleName" is NSArray type where you wants length of the NSArray. But NSArray has not feature "length". NSArray has "Count" feature
//try with this code bellow
NSArray *referralCode = [feed valueForKey:#"code"];
NSLog(#"%#",referralCode);
self.referralCode.text=[referralCode componentsJoinedByString:#" "];//#"," or #"" what event you need
Your feed data is in array. So you have retrieve code value from array.Hope it will help you.
NSMutableArray*feed=[saveDic objectForKey:#"feed"];
NSLog(#"%#",feed);
NSLog(#"%#",[feed valueForKey:#"code"]);
NSString *referralCode = [[feed objectAtIndex:indexPath]valueForKey:#"code"];
NSLog(#"%#",referralCode);
self.referralCode.text=referralCode;

Fetching data from PLIST by Fast Enumeration

I am using for in loop to fetch data from plist.For in loop is correct as it is printing value. But,suddenly, it is showing following exception:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x97a14b0'.
Code:
NSString *sss=[[NSBundle mainBundle]pathForResource:#"s" ofType:#"plist"];
NSDictionary *dic=[[NSDictionary alloc]initWithContentsOfFile:sss];
for(NSArray *arr in [dic allKeys])
{
NSLog(#"%#",arr); // Ii is printing value
NSLog(#"%#",arr[0]); // It is showing exceptions
}
Plist:
There is some problem with NSLog(#"%#",arr[0]);. I want to print values of first index of arrays a and b.
here [dic allKeys] will be keys in dic that are NSString like "a", "b" etc.
so for your plist the code should be,
NSString *sss=[[NSBundle mainBundle]pathForResource:#"s" ofType:#"plist"];
NSDictionary *dic=[[NSDictionary alloc]initWithContentsOfFile:sss];
for(NSString *key in [dic allKeys])
{
NSLog(#"%#",key);
NSArray *value = [dic objectForKey:key];
NSLog(#"%#",value[0]);
}

iOS - Terminating app due to uncaught exception 'NSInvalidArgumentException'

So I am really new at developing for iOS, and I've been doing my best, to search for an answer, to debug it and anything I could come up with.
Though, I haven't been able to find a solution to the issue.
I've been trying to fetch an external JSON document, which works fine, but when it comes to parsing it, it buggers up.
First of all, this is the error message I'm getting, the whole lot of it.
2013-01-31 22:40:19.261 demodh[6205:c07] View Loaded
2013-01-31 22:40:19.479 demodh[6205:c07] -[__NSCFStringcountByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x7554f90
2013-01-31 22:40:19.480 demodh[6205:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x7554f90'
*** First throw call stack:
(0x1c93012 0x10d0e7e 0x1d1e4bd 0x1c82bbc 0x1c8294e 0x28d3 0xbd6589 0xbd4652 0xbd589a 0xbd460d 0xbd4785 0xb21a68 0x4615911 0x4614bb3 0x4652cda 0x1c358fd 0x465335c 0x46532d5 0x453d250 0x1c16f3f 0x1c1696f 0x1c39734 0x1c38f44 0x1c38e1b 0x1bed7e3 0x1bed668 0x14ffc 0x1d6d 0x1c95 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb)
And this is the code I'm using at the moment:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
for(NSDictionary *dict in allDataDictionary)
{
if (![allDataDictionary isKindOfClass:[NSDictionary class]])
{
NSLog(#"2Unable to process temp array because it's an instance of %#", [allDataDictionary class]);
}
else
{
for(NSDictionary *deal in dict)
{
if (![deal isKindOfClass:[NSDictionary class]])
{
NSLog(#"Unable to process temp array because it's an instance of %#", [deal class]);
}
else
{
NSString *title = [deal objectForKey:#"title"];
NSLog(title);
}
}
}
}
}
And the JSON I'm loading is: Link
I hope you're able to assist me in finding a solution.
The problem is you're fast-enumerating a NSDictionary and expecting to get its values, when in fact you'll get its keys. When you try to fast-enumerate an NSString you get the assertion you're seeing. You probably wanted this:
for(NSObject *key in allDataDictionary) {
NSDictionary *dict = allDataDictionary[key];
...
for (NSObject *dealKey in dict) {
NSDictionary *deal = dict[dealKey];
}
...
Alternatively, if you really want to enumerate the values and don't need the keys:
for(NSDictionary *dict in [allDataDictionary allValues]) {
...

NSGenericException when trying to remove an object from an array in iOS

I have checked the examples of how to fix this problem but I am still facing it..
My Code is
NSEnumerator *enu= [obstacles objectEnumerator];
NSMutableArray *delete = [[NSMutableArray alloc] init];
Object *obj;
while ((obj=[enu nextObject])!=nil)
{
if ([obj isKindOfClass: [BObject class]] && CGPointEqualToPoint(obj.position, point) ) {
[view.objects removeObject: obj];
//[obstacles removeObject: obj];
[delete addObject:obj];
}
}
[obstacles removeObjectsInArray:delete];
[delete release];
the error is *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x6a97ba0> was mutated while being enumerated.'
Update: My code work if there is only one object in my obstacles array. it fails with multiple objects..

Resources