NSXmlParser not parsing xml correctly sometime - iphone

when I call API and parse response xml, sometime I got this error ..
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSXMLParser stringByAppendingString:]: unrecognized selector sent to instance 0x6a22080'
*** Call stack at first throw:
(
0 CoreFoundation 0x014c7be9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x012bc5c2 objc_exception_throw + 47
2 CoreFoundation 0x014c96fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x01439366 ___forwarding___ + 966
4 CoreFoundation 0x01438f22 _CF_forwarding_prep_0 + 50
5 CityDeal24 0x0002520e -[CategoryXmlHandler parser:foundCharacters:] + 112
6 Foundation 0x001e8dd4 _characters + 235
7 libxml2.2.dylib 0x018371fb xmlParseCharData + 287
8 libxml2.2.dylib 0x01845a6f xmlParseChunk + 3730
9 Foundation 0x001e921a -[NSXMLParser parse] + 321
10 CityDeal24 0x00025030 -[CategoryXmlHandler parseXML:apiUrl:] + 444
11 CityDeal24 0x00004ac6 -[DagenDealsViewController downloadCategories] + 227
12 CityDeal24 0x00006708 -[DagenDealsViewController doInBackgroundWhenViewWillLoad] + 114
13 Foundation 0x0011ad4c -[NSThread main] + 81
14 Foundation 0x0011acd8 __NSThread__main__ + 1387
15 libSystem.B.dylib 0x9821c85d _pthread_start + 345
16 libSystem.B.dylib 0x9821c6e2 thread_start + 34
)
terminate called after throwing an instance of 'NSException'

Please check that, all the string data have values or not.
If some string value will be nil then you will get such error. So, include the condition for null value also.

As the error says, you are calling stringByAppendingString: on an NSXMLParser. Search your project for stringByAppendingString: and make sure you are calling it on a string.

Look like, you are calling stringByAppendingString method on the object of NSXMLParser.
EDITED:
I assume, you have declared currentElement in your .h as NSMutableArray and alloced that in init function like below.
currentElement = [NSMutableArray alloc] initWithCapacity:10];
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
[currentElement appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
NSLog(#"%#",string);
}

Related

NSarray of NSdictionaries

I am loading some values in UItableview from an array of dictionary values. I then alter the dictionary in the array by adding one more key value object as below
NSMutableDictionary *rowDict = [tableList objectAtIndex:arrayindex];
[rowDict setObject:#"download successfull" forKey:#"downloadstatus"];
but after this when I try retrieveing value from dictionary in the array as below
NSMutableDictionary *rowDict = [tableList objectAtIndex:arrayindex];
NSString *SelectedState = (NSString*)[rowDict objectForKey:#"downloadstatus"];
it crashes ... can any one help me out to fix this
this is the crash display on my consol
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMutableData objectForKey:]: unrecognized selector sent to instance 0x61a8270'
*** Call stack at first throw:
(
0 CoreFoundation 0x003b0be9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x015c15c2 objc_exception_throw + 47
2 CoreFoundation 0x003b26fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00322366 ___forwarding___ + 966
4 CoreFoundation 0x00321f22 _CF_forwarding_prep_0 + 50
5 SifyMyStorage 0x0003b35b -[DownloadListViewController tableView:cellForRowAtIndexPath:] + 314
6 UIKit 0x00ec67fa -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 634
7 UIKit 0x00ebc77f -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75
8 UIKit 0x00ed1450 -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1561
9 UIKit 0x00ec9538 -[UITableView layoutSubviews] + 242
10 QuartzCore 0x009f4451 -[CALayer layoutSublayers] + 181
11 QuartzCore 0x009f417c CALayerLayoutIfNeeded + 220
12 QuartzCore 0x009ed37c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
13 QuartzCore 0x009ed0d0 _ZN2CA11Transaction6commitEv + 292
14 QuartzCore 0x00a1d7d5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
15 CoreFoundation 0x00391fbb __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
16 CoreFoundation 0x003270e7 __CFRunLoopDoObservers + 295
17 CoreFoundation 0x002efbd7 __CFRunLoopRun + 1575
18 CoreFoundation 0x002ef240 CFRunLoopRunSpecific + 208
19 CoreFoundation 0x002ef161 CFRunLoopRunInMode + 97
20 GraphicsServices 0x02ead268 GSEventRunModal + 217
21 GraphicsServices 0x02ead32d GSEventRun + 115
22 UIKit 0x00e6142e UIApplicationMain + 1160
23 SifyMyStorage 0x000020b8 main + 102
24 SifyMyStorage 0x00002049 start + 53
)
terminate called after throwing an instance of 'NSException'
See if you have allocated the object and also get the count of dictionary , if the value is being added also see if u have declared NSmutabledictionary or just NSdictionary ... a view at your class having the code would be more helpful to sort out your problem
Ok, a couple things:
-the code you posted is fine. That's not the issue. The (NSString *) cast is unnecessary, but not an issue.
-there is an issue with your NSArray tablelist. If you are really adding only NSMutableDictionary to the array, then either you are doing that wrong, or your array is going out of scope and when you think you are accessing your array, you are accessing something else in memory at that location.
-How are you retaining your reference to tablelist within the controller?
-Change your second code block to this and I'll bet you find your problem (retainCount on one of these object == 0):
NSLog(#"retain count=%d",[tableList retainCount]);
NSMutableDictionary *rowDict = [tableList objectAtIndex:arrayindex];
NSLog(#"retain count=%d",[rowDict retainCount]);
NSString *SelectedState = [rowDict objectForKey:#"downloadstatus"];
you r not alloc the NSMutableDictionary so the NSMutableDictionary is empty and your app crashes.
NSMutableDictionary *rowDict = [[NSMutableDictionary alloc]init];
rowDict = [tableList objectAtIndex:arrayindex];
NSString *SelectedState = [rowDict objectForKey:#"downloadstatus"];
best of luck

Is there any reason why using an NSMutableDictionary would crash NSNotificationCenter?

When posting notifications with NSNotificationCenter, is there any reason why passing in a NSMutableDictionary instead of an NSDictionary as the userInfo could cause a crash?
- (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;
I'm seeing the following crash log:
Exception Type: SIGSEGV
Exception Codes: SEGV_ACCERR at 0xffffffffe0000008
0 libobjc.A.dylib 0x31516fbc objc_msgSend + 16
1 Foundation 0x3195b50f __57-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke_0 + 19
2 CoreFoundation 0x37a02577 ___CFXNotificationPost_block_invoke_0 + 71
3 CoreFoundation 0x3798e0cf _CFXNotificationPost + 1407
4 Foundation 0x318cf3fb -[NSNotificationCenter postNotificationName:object:userInfo:] + 67
It seems that your observer is crashing the app. Did you remove it from observer list before dealloc ?

How to understand string or array in the web service answer

I get to array value from web service. This array is 1 or more than 1-element array.If tempArra is 1 dimesional array and then If I want to transfer the data in the array to another array(garbageDatesFor01) Then I get the error
EDIT: returning to the web service responses in 2 ways
RESPONSE 1
(
(
"2011-08-03",
"2011-08-17"
)
)
OR
RESPONSE 2
2011-08-04
NSArray *garbageDatesFor01=[[NSArray alloc] initWithArray:tempArr];
2011-08-26 18:43:35.689 AOK[1846:207] -[NSCFString count]: unrecognized selector sent to instance 0x990d8f0
2011-08-26 18:43:35.691 AOK[1846:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString count]: unrecognized selector sent to instance 0x990d8f0'
*** Call stack at first throw:
(
0 CoreFoundation 0x015d25a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x01726313 objc_exception_throw + 44
2 CoreFoundation 0x015d40bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x01543966 ___forwarding___ + 966
4 CoreFoundation 0x01543522 _CF_forwarding_prep_0 + 50
5 CoreFoundation 0x01535d87 -[NSArray initWithArray:] + 39
6 TwenteMilieu 0x0001323a -[ForgottenContainerT1 connectionDidFinishLoading:] + 3750
7 Foundation 0x00113112 -[NSURLConnection(NSURLConnectionReallyInternal) sendDidFinishLoading] + 108
8 Foundation 0x0011306b _NSURLConnectionDidFinishLoading + 133
9 CFNetwork 0x0117148e _ZN19URLConnectionClient23_clientDidFinishLoadingEPNS_26ClientConnectionEventQueueE + 220
10 CFNetwork 0x0123c6e1 _ZN19URLConnectionClient26ClientConnectionEventQueue33processAllEventsAndConsumePayloadEP20XConnectionEventInfoI12XClientEvent18XClientEventParamsEl + 293
11 CFNetwork 0x01167c80 _ZN19URLConnectionClient13processEventsEv + 100
12 CFNetwork 0x01167acf _ZN17MultiplexerSource7performEv + 251
13 CoreFoundation 0x015b38ff __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
14 CoreFoundation 0x0151188b __CFRunLoopDoSources0 + 571
15 CoreFoundation 0x01510d86 __CFRunLoopRun + 470
16 CoreFoundation 0x01510840 CFRunLoopRunSpecific + 208
17 CoreFoundation 0x01510761 CFRunLoopRunInMode + 97
18 GraphicsServices 0x01b411c4 GSEventRunModal + 217
19 GraphicsServices 0x01b41289 GSEventRun + 115
20 UIKit 0x0037fc93 UIApplicationMain + 1160
21 TwenteMilieu 0x00002644 main + 102
22 TwenteMilieu 0x000025d5 start + 53
)
terminate called after throwing an instance of 'NSException'
Current language: auto; currently objective-c
The important line is :
2011-08-26 18:43:35.689 TwenteMilieu[1846:207] -[NSCFString count]: unrecognized selector sent to instance 0x990d8f0
It means you are calling count on a string. The object you think is an array is actually a string.
From looking at your code, tempArr could be either an array or a string. Try this:
if ([tempArr isKindOfClass:[NSArray class]])
{
// Handle array case
}
else if ([tempArr isKindOfClass:[NSString class]])
{
// Handle string case
}
It's probably a good idea to change the name of tempArr to something else, like tempResponse or similar.

how to parse a JSON string in iphone Objective - C?

Hi i am trying to parse a JSON string in iphone and so far i have been able to get JSON VALUE correctly
but after that i am geting an error:
-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x62242e0
2011-08-16 16:11:58.792 BleepBleep[4083:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x62242e0'
*** Call stack at first throw:
(
0 CoreFoundation 0x010a9be9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x011fe5c2 objc_exception_throw + 47
2 CoreFoundation 0x010ab6fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x0101b366 ___forwarding___ + 966
4 CoreFoundation 0x0101af22 _CF_forwarding_prep_0 + 50
5 BleepBleep 0x0000733f -[Screen1 network:didFinishLoadingWithRequest:data:] + 79
6 BleepBleep 0x0000b7e4 -[WNetwork handleResponse] + 323
7 BleepBleep 0x0000b69b -[WNetwork connectionDidFinishLoading:] + 36
8 Foundation 0x00077172 -[NSURLConnection(NSURLConnectionReallyInternal) sendDidFinishLoading] + 108
9 Foundation 0x000770cb _NSURLConnectionDidFinishLoading + 133
10 CFNetwork 0x01674606 _ZN19URLConnectionClient23_clientDidFinishLoadingEPNS_26ClientConnectionEventQueueE + 220
11 CFNetwork 0x0173f821 _ZN19URLConnectionClient26ClientConnectionEventQueue33processAllEventsAndConsumePayloadEP20XConnectionEventInfoI12XClientEvent18XClientEventParamsEl + 293
12 CFNetwork 0x0166ae3c _ZN19URLConnectionClient13processEventsEv + 100
13 CFNetwork 0x0166acb7 _ZN17MultiplexerSource7performEv + 251
14 CoreFoundation 0x0108b01f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
15 CoreFoundation 0x00fe928b __CFRunLoopDoSources0 + 571
16 CoreFoundation 0x00fe8786 __CFRunLoopRun + 470
17 CoreFoundation 0x00fe8240 CFRunLoopRunSpecific + 208
18 CoreFoundation 0x00fe8161 CFRunLoopRunInMode + 97
19 GraphicsServices 0x019de268 GSEventRunModal + 217
20 GraphicsServices 0x019de32d GSEventRun + 115
21 UIKit 0x002e442e UIApplicationMain + 1160
22 BleepBleep 0x00002018 main + 102
23 BleepBleep 0x00001fa9 start + 53
)
terminate called after throwing an instance of 'NSException'
Heres the code i am using in didFinishLoadingWithRequest
-(void)network:(WNetwork*)network didFinishLoadingWithRequest:(NSInteger)pReq data:(NSMutableDictionary*)pData
{
[self removeLoader];
switch (pReq) {
case JBJsonParser:
{
NSArray *parsedString = [pData objectForKey:#"placesname"];
DLog(#"LIST %#",parsedString);
}
break;
default:
break;
}
}
in the network class i am using dis code:
{
SBJSON *parser = [SBJSON new];
NSString *dataString = [[NSString alloc] initWithData:mRespData encoding:NSUTF8StringEncoding];
NSMutableDictionary *newDic = [dataString JSONValue];
if ([(id)mDelegate respondsToSelector:#selector(network:didFinishLoadingWithRequest:data:)]) {
[self.mDelegate network:self didFinishLoadingWithRequest:mReqType data:newDic];
}
[newDic autorelease];
[dataString release];
[parser release];
}
Little bit of JSON:
This is a JSON array:
["firstValue", "secondValue"]
This is a JSON dictionary:
{
"A key" : "A value",
"Another key" : "Another value"
}
Your JSON is telling the parser that the root type is an array. Therefore, jsonValue is returning an array.
You are trying to call objectForKey (NSDictionary method) on that array. That's why the exception was thrown.
Please post your JSON so we can see the structure and how you should parse it. Or, try logging the object you store jsonValue to.
UPDATE:
After reading your JSON, this is how you should parse it:
NSString *jsonString; // set this to your json
NSArray *places = [jsonString jsonValue];
// then iterate through the places, saving off the bits you need
for (NSDictionary *place in places) {
NSString *placeName = [place objectForKey:#"placesname"]; // for example
NSLog(#"Name of place: %#", placeName);
}
What you might want to do is create a custom class called place which has a property for lat, long, placename etc. and then save an array of those.
This post should help.
There are some good examples here:
http://iosdevelopertips.com/cocoa/json-framework-for-iphone-part-2.html
JSON syntax represents both arrays and dictionaries. When parsing an "unknown" piece of JSON code, you don't know whether a given "layer of the onion" is an array or dictionary, so you must check (at each level) to see what kind of object you have. Use [myObject isKindOfClass:[NSArray class]] and [myObject isKindOfClass:[NSDictionary class]].
It's also not unwise to do this checking even with "known" JSON sources, since web sites can break or change, and it's better to present a nice error message (and blame the web site) rather than have your app crash.

Why does this error occur?

i am parsing the data from web service.After parsing the few records it gives this error.Somebody please the tell me what the reason behind this error?
[NSXMLParser length]: unrecognized selector sent to instance 0x6e6d340
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSXMLParser length]: unrecognized selector sent to instance 0x6e6d340'
*** Call stack at first throw:
(
0 CoreFoundation 0x026c4919 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x028125de objc_exception_throw + 47
2 CoreFoundation 0x026c642b -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x02636116 ___forwarding___ + 966
4 CoreFoundation 0x02635cd2 _CF_forwarding_prep_0 + 50
5 Foundation 0x001053e9 -[NSXMLParser parse] + 104
6 SexOffenders 0x00009c30 -[UserProfileVC connectionDidFinishLoading:] + 565
7 Foundation 0x0006e666 -[NSURLConnection(NSURLConnectionReallyInternal) sendDidFinishLoading] + 108
8 Foundation 0x0006e5bf _NSURLConnectionDidFinishLoading + 133
9 CFNetwork 0x02c569f1 _ZN19URLConnectionClient23_clientDidFinishLoadingEPNS_26ClientConnectionEventQueueE + 285
10 CFNetwork 0x02d1fc72 _ZN19URLConnectionClient26ClientConnectionEventQueue33processAllEventsAndConsumePayloadEP20XConnectionEventInfoI12XClientEvent18XClientEventParamsEl + 402
11 CFNetwork 0x02d200ea _ZN19URLConnectionClient26ClientConnectionEventQueue33processAllEventsAndConsumePayloadEP20XConnectionEventInfoI12XClientEvent18XClientEventParamsEl + 1546
12 CFNetwork 0x02d200ea _ZN19URLConnectionClient26ClientConnectionEventQueue33processAllEventsAndConsumePayloadEP20XConnectionEventInfoI12XClientEvent18XClientEventParamsEl + 1546
13 CFNetwork 0x02c4bdfe _ZN19URLConnectionClient13processEventsEv + 100
14 CFNetwork 0x02c4bc95 _ZN17MultiplexerSource7performEv + 247
15 CoreFoundation 0x026a5d7f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
16 CoreFoundation 0x026041dd __CFRunLoopDoSources0 + 333
17 CoreFoundation 0x026037c6 __CFRunLoopRun + 470
18 CoreFoundation 0x02603280 CFRunLoopRunSpecific + 208
19 CoreFoundation 0x026031a1 CFRunLoopRunInMode + 97
20 GraphicsServices 0x02f292c8 GSEventRunModal + 217
21 GraphicsServices 0x02f2938d GSEventRun + 115
22 UIKit 0x002dab58 UIApplicationMain + 1160
23 SexOffenders 0x00002198 main + 102
24 SexOffenders 0x00002129 start + 53
)
terminate called after throwing an instance of 'NSException'
'-[NSXMLParser length]: unrecognized selector sent to instance 0x6e6d340
this shows you are calling a function which is not a member function of NSXMLParser class.
actually in your code you are calling any method which can not be invoke on the obeject of NSXMLParser class.
chechk by debugging pointer.
I just suffered from this same issue. For me the problem was caused by this line of code:
operation.responseSerializer = [AFXMLParserResponseSerializer serializer];
This serialised the responseObject I was passing into the parser to XML but the parser expects an NSData object. Simply getting rid of that line solved the problem.
I also faced the similar problem.
Fixed by changing my line
self.xmlParser = [[NSXMLParser alloc] initWithData:data];
to
self.xmlParser = (NSXMLParser *)responseObject;
I found my solution from this blog
Raywanderlick Blog
Explanation for Error
This configuration of [AFXMLParserResponseSerializer serializer] already return an initialized object and I was initializing it again using already initialized parser by considering it data object.
Commenting following line didn't work for me as suggested by #Hodson answer above and it doesn't make sense as this is default configuration of AFTNetworking library.
operation.responseSerializer = [AFXMLParserResponseSerializer serializer];