NSArrayM unrecognized selector error - ios

I need some help understanding why I am getting an unrecognized selector error
I have a NSMutableDictionary
#interface CallDetailViewController () {
NSMutableDictionary * thisDictionary;
}
#end
That gets populated from the MasterTabController
- (void) some method {
thisDictionary = [[(MasterTabController *)self.tabBarController detailDictionary] mutableCopy];
NSLog(#"Check dictinoary: %#", [thisDictionary description]);
}
Outputting a description shows the data is there
Check dictinoary: (
{
DATEIN = "2015-12-11 13:33:41";
"ETA" = "2015-12-14 13:54:16";
RecordKey = 2961;
Destination = "Some address";
Location = "Some location";
} )
But when I try to access an object in it:
NSLog(#"Destination: %#", [thisDictionary objectForKey:#"Destination"]);
I get the following error.
-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x7a73f9a0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]:
unrecognized selector sent to instance 0x7a73f9a0'
The array is populated from a JSON value that returns an array of records called Record. After the JSON is returned, I pull out just section that contains the record.
NSDictionary * dictTowx = [NSDictionary dictionaryWithDictionary:[dictJSON objectForKey:#"ThisResponse"]];
NSDictionary * dictData = [NSDictionary dictionaryWithDictionary:[dictTowx objectForKey:#"Data"]];
NSArray * arrRecordSet = [dictData objectForKey:#"Recordset"];
NSDictionary * dicRecord= [arrRecordSet objectAtIndex:0];
self.detailDictionary = [dicRecord objectForKey:#"Record"];
The RAW JSON:
{
ThisResponse = {
Data = {
Recordset = (
{
Record = (
{
DATEIN = "2015-12-11 13:33:41";
"ETA" = "2015-12-14 13:54:16";
RecordKey = 2961;
Destination = "Some address";
Location = "Some location";
}
);
}
);
};
};
}
What am I doing wrong?

Please try to assign like this
self.detailDictionary = [[dicRecord objectForKey:#"Record"] objectAtIndex:0];
and then access like this
NSLog(#"Destination: %#", [thisDictionary objectForKey:#"Destination"]);
Enjoy programming.

- (void) some method {
thisDictionary = [[(MasterTabController *)self.tabBarController detailDictionary] mutableCopy];
NSLog(#"Check dictinoary: %#", [thisDictionary description]);
}
try replacing
thisDictionary = [[(MasterTabController *)self.tabBarController detailDictionary] mutableCopy];
with
thisDictionary = [[NSMutableDictionary alloc]initWithDictionary: [[{MasterTabController *)self.tabBarController detailDictionary] mutableCopy]]];
i think your problem may be you are not instantiating the dictionary before setting it to objects from another.

Related

'NSInvalidArgumentException', reason: '-[NSTaggedPointerString objectForKey:]: unrecognized selector sent to instance 0xa000000617461644'

I am developing simple app that gets json data and stores into different variables
here is my code that gets json data
if ([method isEqualToString:#"getmobiledata"]){
defaultDict = [[NSMutableDictionary alloc]init];
[defaultDict addEntriesFromDictionary:[NSJSONSerialization JSONObjectWithData:[data dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil]];
mobile = [defaultDict objectForKey:#"data"];
}
here is my son data:
{
data = (
{
id = 1;
package = 819MB;
rate = "$1";
type = Somnet;
},
{
id = 2;
package = "1,638MB";
rate = "$2";
type = Somnet;
},
);
}
here is paring json into nstring :
for(NSDictionary *getData in mobile){
NSString *idno = [getData objectForKey : #"id"];
NSString *package = [getData objectForKey :#"package"];
NSString *rate = [getData objectForKey :#"rate"];
NSString *type = [getData objectForKey :#"type"];
}
please help me how to solve this
The data model will collide with the same name as the system property ID, and it will collide and crash.
You can use a new ID like this:
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
if([key isEqualToString:#"id"])
self.NewId = value;
}

Error while getting value from NSDictionary

I'm trying to get from JSON parsed in NSDictionary and I'm getting this error all the time.
NSDictionary:
{
"admin_id" = 191767626;
body = "\U043a\U043b\U0430\U0441\U0441, \U043f\U043e\U0447\U0442\U0438 \U0434\U043e\U0434\U0435\U043b\U0430\U043b \U043f\U043e\U0434\U0434\U0435\U0440\U0436\U043a\U0443 \U0433\U0440\U0443\U043f\U043f\U043e\U0432\U044b\U0445 \U0431\U0435\U0441\U0435\U0434";
"chat_active" = "191767626,111273042,108237840,178159348,119271375,90588741,131506543,12657348,251374070,77508447,257350143,222456587,133059311,119425760,95255813,138571437,183017202,57733104,277277624,166958749,138127148,146374071,26326487,71995910,141152531,169957302,148888167,350803848,381660274,94296816,388139636";
"chat_id" = 104;
date = 1488494694;
mid = 1141349;
out = 1;
"photo_100" = "https://pp.userapi.com/c637621/v637621626/2317d/K9M4thCXrP0.jpg";
"photo_200" = "https://pp.userapi.com/c637621/v637621626/2317b/u5opnbkPQo0.jpg";
"photo_50" = "https://pp.userapi.com/c637621/v637621626/2317e/tNBXXHah700.jpg";
"push_settings" = {
"disabled_until" = 1773859745;
sound = 0;
};
"read_state" = 1;
title = CJB;
uid = 79372051;
"users_count" = 32;
}
So I'm interested in getting values for keys chat_id, title and users_count
I tried doing
NSLog(#"%#", [chatInfo objectForKey:#"chat_id"]);
But I'm getting this error
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber objectForKey:]: unrecognized selector sent to instance 0x19478910'
How can I solve this? Thanks in advance!
Try like this :
NSLog(#"%#", [[chatInfo objectForKey:#"chat_id"] stringValue]);
Or
NSLog(#"%#",[NSString stringWithFormat:#"%#", [[chatInfo objectForKey:#"chat_id"] stringValue]]);
Hope this will work for you.
Try this and use breakpoints on NSLogs for understand what is wrong:
if ([chatInfo isKindOfClass:[NSArray class]]) {
NSArray *array = (NSArray *) chatInfo;
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([destinationVC isKindOfClass:[NSDictionary class]]) {
NSDictionary *dictionary = (NSDictionary *) destinationVC;
if (dictionary[#"chat_id"]) {
NSLog(#"chat_id: %#", dictionary[#"chat_id"]);
} else {
NSLog(#"chat_id: not found");
}
} else {
NSLog(#"its not dictionary, check your JSON mapping");
}
}];
} else {
NSLog(#"This is not NSArray!");
}

iOS app crashing with error: NSCFNumber stringByReplacingOccurrencesOfString:withString: unrecognized selector sent to instance

Our iOS app terminates with an error of [__NSCFNumber stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x1755e090.
We're testing on an iPhone 5 w iOS 7.
It crashes on this line: [self parseDictionary:notificationMessage intoJSON:jsonStr].
Method containing this line:
- (void)notificationReceived {
NSLog(#"Notification received");
if (notificationMessage && self.callback)
{
NSMutableString *jsonStr = [NSMutableString stringWithString:#"{"];
[self parseDictionary:notificationMessage intoJSON:jsonStr];
if (isInline)
{
[jsonStr appendFormat:#"foreground:\"%d\"", 1];
isInline = NO;
}
else
[jsonStr appendFormat:#"foreground:\"%d\"", 0];
[jsonStr appendString:#"}"];
NSLog(#"Msg: %#", jsonStr);
NSString * jsCallBack = [NSString stringWithFormat:#"%#(%#);", self.callback, jsonStr];
[self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];
self.notificationMessage = nil;
}
}
-(void)parseDictionary:(NSDictionary *)inDictionary intoJSON:(NSMutableString *)jsonString
{
NSArray *keys = [inDictionary allKeys];
NSString *key;
for (key in keys)
{
id thisObject = [inDictionary objectForKey:key];
if ([thisObject isKindOfClass:[NSDictionary class]])
[self parseDictionary:thisObject intoJSON:jsonString];
else
[jsonString appendFormat:#"\"%#\":\"%#\",",
key,
[[[[inDictionary objectForKey:key]
stringByReplacingOccurrencesOfString:#"\\" withString:#"\\\\"]
stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""]
stringByReplacingOccurrencesOfString:#"\n" withString:#"\\n"]];
}
}
Stack trace (along with one debugging line to show the variable value):
2014-01-07 16:32:36.980 Wopple[195:60b] Notification received
Printing description of self->notificationMessage:
{
"_" = "gHf8EeO3_ZDiugJkgA";
aps = {
alert = "hi foo bar right";
badge = 3;
};
}
2014-01-07 16:33:22.774 Wopple[195:60b] -[__NSCFNumber stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x1755e090
2014-01-07 16:33:22.776 Wopple[195:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x1755e090'
*** First throw call stack:
(0x308c3e83 0x3ac246c7 0x308c77b7 0x308c60af 0x30814dc8 0x88da5 0x88d1f 0x88915 0x8757b 0x3335ec93 0x3335f75d 0x340d0b37 0x3088e777 0x3088e713 0x3088cedf 0x307f7471 0x307f7253 0x3552b2eb 0x330ac845 0x7f5d3 0x3b11dab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
When you're using:
[[[[inDictionary objectForKey:key]
stringByReplacingOccurrencesOfString:#"\\" withString:#"\\\\"]
stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""]
stringByReplacingOccurrencesOfString:#"\n" withString:#"\\n"]];
You're assuming that objectForKey is going to return an NSString object. But, in the case where your app is crashing, the object returned is actually an NSNumber.
You should use isKindOfClass: to determine the object type, or use stringValue on your NSNumber to get a string representation of the object.

How to correct extract string from NSDictionary

I have A XML string like this:
<result>
<id>1032</id>
<name>SOME NAME</name>
<price>25000</price>
<img>http://somesite/some/some.png</img>
<detail_text>DESCRIPTION</detail_text>
</result>
After parsing I get NSDictionary object like this:
result = {
"detail_text" = {
text = DESCRIPTION";
};
id = {
text = 1032;
};
img = {
text = "http://somesite/some/some.png";
};
name = {
text = "SOME NAME";
};
price = {
text = 25000;
};
};
}
And I want to have access to NSDictionary elements and save then like a string. When it was many elements ( image1 image2 ...) I using this:
_PtitleArr =[[NSArray alloc] initWithArray:[[[_PxmlDictionary objectForKey:#"result"] objectForKey:#"name"] valueForKey:#"text"]];
And all works, but now only one element in it, and this way doesn't work any more. I'm trying do this: _PtitleArr =[[[NSString alloc] initWithString:[_PxmlDictionary objectForKey:#"result"] objectForKey:#"name"] valueForKey:#"text"];
Or so: NSString *title = [[_PxmlDictionary objectAtIndex:0 (or "1")] objectForKey:#"name"]; But always xCode give me an error:
-[__NSCFString count]: unrecognized selector sent to instance 0x8995070
2013-11-15 08:16:41.276 Akc_ver_0.1[8670:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString count]: unrecognized selector sent to instance 0x8995070'
What I'm doing wrong and how can I extract elements correctly?
Your xml format looks like it contains dictionary inside dictionary. So try to access like that below
NSDictionary * dict=[_PxmlDictionary objectForKey:#"result"] ;
Now try to fetch the dictionary value in NSString
NSDictionary *dict1=[dict objectForKey:#"name"];
NSString *str=[dict1 objectForKey:#"text"];

Get Row from NSArray

Hello i get a json that looks like this:
features: (
{
attributes = {
Gecontroleerd = Ja;
};
geometry = {
x = "5.968097965285907";
y = "52.50707112779077";
};
}
)
From this code:
NSDictionary *root = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
NSArray *data = [root objectForKey:#"features"];
NSLog(#"features: %#", data );
for (NSArray *row in data) {
NSString *latitude = row[5];
NSString *longitude = row[7];
NSString *crimeDescription = #"test";
NSString *address = #"banaan";
And u need to x values 5.968097965285907 for latitude
and y values 52.50707112779077 for longitude
But i get this error:
[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x14831450
2012-11-14 10:10:59.000 ArrestPlotter[6330:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x14831450'
*** First throw call stack:
(0x1860012 0x1655e7e 0x18eb4bd 0x184fbbc 0x184f94e 0x2dbc 0x3b8f 0x1cc1e 0x16696b0 0xfa0035 0x17e3f3f 0x17e396f 0x1806734 0x1805f44 0x1805e1b 0x224a7e3 0x224a668 0x4a765c 0x25bd 0x24e5 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb)
Does anyone wich row i need to select?
I guess that the only thing is that the row number needs to be changed. Or maybe there should be something like this : [1][5]. Im not quite sure how this works
NSArray *data = [root objectForKey:#"features"];
NSLog(#"features: %#", data );
for (NSDictionary *dic in data) {
NSDictionary geometry = [dic objectForKey:#"geometry"];
// Do what you want..
NSString *myAwesomeX = [geometry objectForKey:#"x"];
NSString *myAwesomeY = [geometry objectForKey:#"y"];
}
The problem here is that you are trying to send a selector message to object row, that is in memory a NSDictionary (NSCFDictionary?) object, and you are trying to manage it like a NSArray.
The method objectAtIndexedSubscript (is underlying called by row[5] and row[7]) exists in NSDictionary, but no in NSArray.
Change
for (NSArray *row in data) {
by
for (NSDictionary *row in data) {
Also, you have to change the management of data inside for, look at the result of your log statement and act accord whit it.
I hope this will help!

Resources