How to correct extract string from NSDictionary - ios

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"];

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;
}

NSArrayM unrecognized selector error

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.

ios - get values from NSDictionary

I have JSON on my server, which is parsed into iOS app to NSDictionary. NSDictionary looks like this:
(
{
text = Aaa;
title = 1;
},
{
text = Bbb;
title = 2;
}
)
My question is - how to get just text from first dimension, so it should be "Aaa". I've tried to use this:
[[[json allValues]objectAtIndex:0]objectAtIndex:0];
But it didn't work, it ends with error
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[__NSCFArray allValues]:
unrecognized selector sent to instance 0x714a050'
So can you help me please, how to get just one value from specified index? Thanks!
That error message is simply telling you that NSDictionary (which is the first object of that array, along with the second) doesn't respond to objectAtIndex.
This will be a bit cody, but it explains it better:
NSArray *jsonArray = [json allValues];
NSDictionary *firstObjectDict = [jsonArray objectAtIndex:0];
NSString *myValue = [firstObjectDict valueForKey:#"text"];
Your JSON object is an array, containing two dictionaries. That's how to get the values:
NSDictionary* dict1= json[0];
NSString* text= dict1[#"text"];
NSString* title= dict1[#"title"];
Try this:
NSString *txt = [[json objectAtIndex:0] objectForKey:#"text"];
UPDATE: Have fixed the error. Thanks yunas.

Parsing NSDictionary data from JSONValue as a string

I just fetched data but when i try to append it to string from NSData , compiler occurs Exception error .
My code seems like this:
NSDictionary *allDAtaDictionary = [NSJSONSerialization JSONObjectWithData:_webData options:0 error:nil];
NSString *jSonStatus = [allDAtaDictionary objectForKey:#"status"];
if([jSonStatus isEqualToString:#"OK"])
{
NSDictionary *results = [allDAtaDictionary objectForKey:#"user_details"];
NSLog(#"User Details : %#",results);
All things works well until these line just starts:
NSString *userID = [results objectForKey:#"userID"];
The error output is :
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]:
unrecognized selector sent to instance 0x9485a00'
and this is my jSOn output , NSLog(#"User Details : %#",results);
User Details : (
{
userCity = California;
userCountry = "United States";
userDOB = "1988-03-02";
userDetails = "";
userEmail = "xxx#xxx.com";
userFBID = 715296184;
userFullName = "John Mc Grager";
userGender = Male;
userID = 70;
userLastLoginTime = "2013-05-29 10:51:27";
userLatitude = "37.7858";
userLongtitude = "-122.406";
userNickName = warblader;
userPassword = "";
userStatus = 1;
userToken = "";
}
)
Seems like the JSON is an array with one dictionary in it. What you show us here at least.
So
NSArray *results = [allDAtaDictionary objectForKey:#"user_details"];
NSLog(#"User Details : %#",results);
assert(results.count>0);
NSDictionary *user = [results objectAtIndex:0];
NSString *userID = [results objectForKey:#"userID"];
NSLog(#"%#", userID);
First of all you need save use the serialised data into an array.. Then you have to use it accordingly .. in this case
NSArray *allDAtaDictionary = [NSJSONSerialization JSONObjectWithData:_webData options:0 error:nil];
Log the array in index 0 and the see what you are getting. After this you can start parsing the data.
The exception is occuring due to this. Check my answer here to parse JSON How to parse JSON with multiple instance in Object C
Your response probably has An array in it, which result Dictionary is being holding, so you need to use ObjectAtIndex rather calling objectForKey

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