I have a NSMuttableArraywhich contains the structure like below,
28 = (
twenty
);
30 = (
thirty
);
32 = (
thirty
);
I am trying to store the values like 28,30,32 into a separate array. I tried loop as,
NSMutableArray *getvalueshere;
for(int i=0;i<getvalueshere.count;i++)
NSArray *getval = [getvalueshere objectAtindex:i];
}
On trying this, my app gets crash saying __NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance.
Your object is a NSDictionary as the error message reveals.
You can get the numbers – which are actually the keys of the dictionary – with
NSArray *getval = [getvalueshere allKeys];
It's saying getvalueshere is an NSDictionary which doesn't recognize objectAtIndex. When you declared getvalueshere, you gave it a NSDictionary like structure. It is considered an NSDictionary even though you explicitly stated it should be an NSMutableArray.
Related
My iOS app terminates with Thread 1: signal SIGABRT error. The strange thing is, when I hover the cursor over the NSNumber object (where the error has occurred), it displays the objects of an NSDictionary with Key/Value pair. Following is my code snippet:
for(id obj in [MainDict allKeys])
{
NSArray *AnArrayInsideMainDict = [MainDict objectForKey:obj];
double i=1;
for(NSUInteger n=0; n<4; n++)
{
NSNumber *anObject = [AnArrayInsideMainDict objectAtIndex:n];
NSNumber *nextObject = [nextDict objectForKey:[NSNumber numberWithDouble:i]];
NSNumber *HereIsTheError = [NSNumber numberWithFloat:(powf(([anObject floatValue]-[nextObject floatValue]),2))];
[ThisIsMutableArray addObject:HereIsTheError];
i++
}
}
Here, the MainDict contains 64 key/value pairs (each key contains an NSArray of 5 objects).nextDictis an NSDictionary with 4 key/value pairs (each key contains one NSNumber object // EDIT: each key actually contained an NSArray with single NSNumber Object, this was the reason for the error). After the app termination and hovering the cursor over the HereIsTheError, I get following key/value pair:
The terminating error at the console is:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM floatValue]: unrecognized selector sent to instance 0x170257a30'
*** First throw call stack:
(0x18b032fe0 0x189a94538 0x18b039ef4 0x18b036f54 0x18af32d4c 0x1000891a0 0x10008bd74 0x10020d598 0x100579a50 0x100579a10 0x10057eb78 0x18afe10c8 0x18afdece4 0x18af0eda4 0x18c979074 0x1911c9c9c 0x1000906b4 0x189f1d59c)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
How can NSNumber conation the objects of NSDictionary? I use Xcode Version 9.0.1 (9A1004) and Objective-C.
As the comments state, but most importantly your error message states, your objects anObject and nextObject are not NSNumber's - they are NSMutableArray's, hence the
-[__NSArrayM floatValue]: unrecognized selector...
part of your error.
Ensure the objects in AnArrayInsideMainDict are in fact
NSNumber's before attempting to cast them as numbers,
I would suggest flagging your objects before "assuming" their types, but I doubt that would help you get your desired outcome (as this would most likely from your case here skip each object that is not an NSNumber).
Before you even enter the for loop in [MainDict allKeys], backtrace to make sure you are in fact passing arrays of NSNumber's as the dictionaries objects.
IF you are actually not sure of the object types, you can just throw a flag to make sure you are not misinterpreting any of the objects:
...
for (NSUInteger n=0; n<4; n++) {
NSNumber *anObject = [AnArrayInsideMainDict objectAtIndex:n];
NSNumber *nextObject = [nextDict objectForKey:[NSNumber numberWithDouble:i]];
if ([anObject isKindOfClass:NSNumber.class] && [nextObject isKindOfClass:NSNumber.class]) {
// Good to continue now that you know the objects
} else NSLog(#"whoops.. anObject: %# nextObject: %#", anObject.class, nextObject.class);
...
LASTLY, if you are so daring, and are sure that somewhere in this dictionary are your NSNumber's, you could flag your steps to check for instances of the NSNumber.class in order to seek out your floats.
Otherwise, I suggest deep diving into the how and where you are getting your MainDict from.
Happy coding - cheers!
This the line i have using to convert the object to integer values,Inside For Loop I have Placed This code
NSInteger tag=[[arrFullSubCategory valueForKey:#"category"] integerValue];
Inside arrFullSubCategory:
(
{
category = 35;
image = "images/Hatchback.jpg";
name = Hatchback;
parent = 20;
},
{
category = 36;
image = "images/Sedan.jpg";
name = Sedan;
parent = 20;
},
{
category = 37;
image = "images/SUV.jpg";
name = SUV;
parent = 20;
}
)
Exception:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI integerValue]: unrecognized selector sent to instance 0x7ff4ba58f930'
arrFullSubCategory is an array and you should reach it's elements first. Than you will have NSDictionary objects. After that you can access your category element. So I think your code should be like that:
for (NSInteger i = 0; i < arrFullSubCategory.count; ++i) {
NSInteger tag=[[[arrFullSubCategory objectAtIndex:i] valueForKey:#"category"] integerValue];
}
Explanation of the error:
The error means you have an array, and arrays don't respond to integerValue.
Your variable arrFullSubCategory references an array (of 3 elements), and each element is a dictionary. If you call valueForKey: on an array of dictionaries then the key lookup is performed for each dictionary and an array is constructed for the results. In your case the result (using literal syntax) is the array:
#[ #35, #36, #37 ]
Whether this array is directly useful to you, or whether you should access the array one element at a time - using a loop or method which calls a block per element, etc. - will depend on what your goal is.
HTH
Try this code inside for loop I hope this help you
NSInteger tag=[[[arrFullSubCategory objectAtIndex:i] valueForKey:#"category"] integerValue];
you have array of dictionary, So you use it given below code
[[[arrFullSubCategory objectAtIndex:] objectForKey:#"category"] integerValue]
I am trying to update a specific key in an NSMutableArray. The array is called ListForTable and I am trying to update the key statusReport. There are 4 objects in the array. I am trying to update the first.The following is causing an error:
[ListForTable replaceObjectAtIndex:[[ListForTable objectAtIndex:0] objectForKey:#"statusReport"] withObject:#"No edits"];
The array is created in the following way:
[ListForTable addObject:[NSDictionary dictionaryWithObjectsAndKeys: itemName, #"itemName", #"", #"statusReport", nil]];
Can anyone explain why?
The error message is:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM replaceObjectAtIndex:withObject:]: index 1087320 beyond bounds [0 .. 3]'
*** First throw call stack:
(0x2c57df87 0x39f1ac77 0x2c49b331 0xbe9bb 0x2f9fc86d 0x2f9fc5dd 0x300511f7 0x2fcc5b51 0x2fcdd933 0x2fcdf8cb 0x2fadc615 0xab051 0x2fa2d497 0x2fa2d439 0x2fa1804d 0x2fa2ce69 0x2fa2cb43 0x2fa26451 0x2f9fccc5 0x2fc70513 0x2f9fb707 0x2c544807 0x2c543c1b 0x2c542299 0x2c48fdb1 0x2c48fbc3 0x337c4051 0x2fa5ba31 0x604d5 0x3a4b6aaf)
libc++abi.dylib: terminating with uncaught exception of type NSException
This is an error:
[ListForTable replaceObjectAtIndex:[[ListForTable objectAtIndex:0] objectForKey:#"statusReport"] withObject:#"No edits"];
You are getting your parameters mixed up.
[ListForTable replaceObjectAtIndex:...]
should be taking an integer
But
[[ListForTable objectAtIndex:0] objectForKey:#"statusReport"]
is probably not an integer.
Rather than replace it, get the pointer to it:
NSMutableDictionary *entry = [[ListForTable objectAtIndex:0] mutableCopy];
and update it:
entry[#"statusReport"] = #"No edits";
and then replace:
[ListForTable replaceObjectAtIndex:0 withObject:[entry copy]];
Logic being: you can't modify an NSDictionary (it is immutable) so you need a mutable copy that you can change. Once you change it, you need to replace it.
the
[entry copy]
bit makes it an immutable dictionary again.
Truthfully, it doesn't look like you want this array to contain immutable objects anyway, but I hope this explains your issues
[ListForTable replaceObjectAtIndex:
[[ListForTable objectAtIndex:0] objectForKey:#"statusReport"]
withObject:#"No edits"];
Second line is your "index" but it appears to be an object of some sort.
Trying to parse your intention, I'm thinking maybe you aren't trying to do:
ary[0] = "something else";
You are trying to modify something in the array?
If so you probably want to do something more like this:
MyCoolObject *o = [ListForTable objectAtIndex: 0];
[o setSomethingOrOther: #"No Edits"];
However if you were just trying to swap out something in the array. You need to do something like:
[ListForTable replaceObjectAtIndex: 0 withObject: #"whatever!"];
Don't call this while iterating over your array. Also don't name a variable starting with a capital letter.
You want to do:
[[ListForTable objectAtIndex: 0] setObject:#"no edits" forKey:#"statusReport"];
I created my own dictionary by taking the values from a json file in the dictionary I have a set of values, the other can take them and use them, instead of these start with a brace can not seem to get them:
weather = (
{
description = "broken clouds";
icon = 04d;
id = 803;
main = Clouds;
}
);
Use this command to take the values in the Dictionary:
NSString *currweather = myDict[#"weather"][#"main"];
The application quits when the launch. How can I fix?
NSString *currweather = myDict[#"weather"][0][#"main"];
The weather key is referencing an array of dictionary.
You should have been given a clue from the error message about an unrecognized selector (objectForKey:) being called on an array class.
I am trying to save an NSMutableArray in CoreData. The Array contains objects NSDictionary
NSDictionary has following Structure
valueDict =
{
FloorId = F0001;
endCoordinates = "NSPoint: {541, 413}";
linePath = "<UIBezierPath: 0x1d0903c0>";
pointsOnLine = (
);
startCoordinates = "NSPoint: {418, 504}";
},
To write to the Core Data I use following code: parray is type BinaryData
points.parray = [NSKeyedArchiver archivedDataWithRootObject:self.locationsArray];
and to retrieve value I use
locationsArray = [NSKeyedUnarchiver unarchiveObjectWithData:points.parray];
When I try to retrieve it i get following error :
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSDictionary initWithObjects:forKeys:]: count of objects (0) differs from count of keys (5)'
I have checked that NSArray and NSDictionary adopts NSCoding protocol. What am I doing wrong here ?
The best way to solve this is to create either a small test app, or a test method called when your app launches. Create a typical dictionary using the same keys and types, try to archive it, log the data size, then immediately try to unarchive the data. Assuming it fails comment out one or more keys til what's left works. Now you know the problem key and can better determine what is wrong.