AFDownloadRequestOperation crashes on 404 response - ios

I created a new project in xcode with only this code to test
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
cachePath = [cachePath stringByAppendingPathComponent:#"blabla.jpg"];
NSString *fileURL = #"http://en.wikipedia.org/dcjhbdkcjc";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:fileURL]];
AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:cachePath shouldResume:YES];
//AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"%#",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"%#",error);
}];
[operation start];
And crashes when 404 is returned with the following error con console, none of the NSLog in the test code are ever called.
2014-08-22 16:51:33.809 AFDownloadTest[7106:3403] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[3]'
*** First throw call stack:
(
0 CoreFoundation 0x0000000101b39495 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010172d99e objc_exception_throw + 43
2 CoreFoundation 0x0000000101b02dce -[__NSPlaceholderDictionary initWithObjects:forKeys:count:] + 382
3 CoreFoundation 0x0000000101b2e23f +[NSDictionary dictionaryWithObjects:forKeys:count:] + 63
4 AFDownloadTest 0x000000010002a955 -[AFHTTPResponseSerializer validateResponse:data:error:] + 1893
5 AFDownloadTest 0x0000000100002cf2 -[AFDownloadRequestOperation connection:didReceiveData:] + 178
6 Foundation 0x000000010141e36b __65-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]_block_invoke + 48
7 Foundation 0x00000001012d1bdb -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] + 210
8 Foundation 0x00000001012d1aec -[NSURLConnectionInternal _withActiveConnectionAndDelegate:] + 69
9 Foundation 0x00000001012d1e37 _NSURLConnectionDidReceiveData + 79
10 CFNetwork 0x0000000106ae8e05 ___ZN27URLConnectionClient_Classic23_delegate_cacheTrifectaEPK20_CFCachedURLResponseU13block_pointerFvvE_block_invoke + 778
11 CFNetwork 0x0000000106ae4802 ___ZN27URLConnectionClient_Classic18_withDelegateAsyncEPKcU13block_pointerFvP16_CFURLConnectionPK33CFURLConnectionClientCurrent_VMaxE_block_invoke_2 + 84
12 CoreFoundation 0x0000000101adff74 CFArrayApplyFunction + 68
13 CFNetwork 0x0000000106a573e7 _ZN19RunloopBlockContext7performEv + 133
14 CFNetwork 0x0000000106a57217 _ZN17MultiplexerSource7performEv + 247
15 CFNetwork 0x0000000106a5703a _ZN17MultiplexerSource8_performEPv + 72
16 CoreFoundation 0x0000000101ac8d21 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
17 CoreFoundation 0x0000000101ac85f2 __CFRunLoopDoSources0 + 242
18 CoreFoundation 0x0000000101ae446f __CFRunLoopRun + 767
19 CoreFoundation 0x0000000101ae3d83 CFRunLoopRunSpecific + 467
20 Foundation 0x0000000101339fce -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 253
21 Foundation 0x00000001012dfdc0 -[NSRunLoop(NSRunLoop) run] + 74
22 AFDownloadTest 0x0000000100011fe8 +[AFURLConnectionOperation networkRequestThreadEntryPoint:] + 280
23 Foundation 0x00000001013352df __NSThread__main__ + 1167
24 libsystem_pthread.dylib 0x000000010236e2fc _pthread_body + 131
25 libsystem_pthread.dylib 0x000000010236e279 _pthread_body + 0
26 libsystem_pthread.dylib 0x000000010236c4b1 thread_start + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
Halting the app at the line 169 of the file AFURLConnectionOperation.m
which is [runLoop run];

This was a bug on the AFDownloadRequestOperation code, was fixed on this commit a36bd98208, on sept 4, 2014

Related

IOS Can't Set UIImage From Facebook Graph Request

I make a Facebook graph request like so, than I try to set a UIImageView From the result from the Facebook Graph Request.
NSString *path = [NSString stringWithFormat:#"http://graph.facebook.com/%#/picture?type=large", userID];
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:path
parameters:nil
HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
cell.profilePicture.image = result;
}];
However the app crashes and gives me this error report?
2015-07-01 15:31:02.573 Hang[1916:45710] -[__NSCFDictionary size]: unrecognized selector sent to instance 0x7fb29a5658b0
2015-07-01 15:31:02.578 Hang[1916:45710] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary size]: unrecognized selector sent to instance 0x7fb29a5658b0'
*** First throw call stack:
(
0 CoreFoundation 0x000000010bc75c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010b90ebb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010bc7d0ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010bbd313c ___forwarding___ + 988
4 CoreFoundation 0x000000010bbd2cd8 _CF_forwarding_prep_0 + 120
5 UIKit 0x000000010c1f178b -[UIImageView _updateImageViewForOldImage:newImage:] + 570
6 UIKit 0x000000010c1ee27e -[UIImageView setImage:] + 316
7 Hang 0x0000000109e3b540 __66-[CollectionViewController collectionView:cellForItemAtIndexPath:]_block_invoke + 160
8 Hang 0x0000000109e5e6ef -[FBSDKGraphRequestMetadata invokeCompletionHandlerForConnection:withResults:error:] + 146
9 Hang 0x0000000109e64a5d __64-[FBSDKGraphRequestConnection processResultBody:error:metadata:]_block_invoke370 + 142
10 Hang 0x0000000109e647f2 -[FBSDKGraphRequestConnection processResultBody:error:metadata:] + 1046
11 Hang 0x0000000109e642c0 __64-[FBSDKGraphRequestConnection completeWithResults:networkError:]_block_invoke + 428
12 CoreFoundation 0x000000010bbabf22 __53-[__NSArrayM enumerateObjectsWithOptions:usingBlock:]_block_invoke + 114
13 CoreFoundation 0x000000010bbab67c -[__NSArrayM enumerateObjectsWithOptions:usingBlock:] + 300
14 Hang 0x0000000109e64077 -[FBSDKGraphRequestConnection completeWithResults:networkError:] + 670
15 Hang 0x0000000109e63364 -[FBSDKGraphRequestConnection completeFBSDKURLConnectionWithResponse:data:networkError:] + 669
16 Hang 0x0000000109e61a92 __36-[FBSDKGraphRequestConnection start]_block_invoke_2 + 72
17 Hang 0x0000000109e5ee20 -[FBSDKURLConnection invokeHandler:error:response:responseData:] + 85
18 Hang 0x0000000109e5ed95 -[FBSDKURLConnection logAndInvokeHandler:response:responseData:] + 395
19 Hang 0x0000000109e5f0e6 -[FBSDKURLConnection connectionDidFinishLoading:] + 135
20 CFNetwork 0x000000010add10bc __65-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]_block_invoke + 69
21 CFNetwork 0x000000010add1060 -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] + 199
22 CFNetwork 0x000000010add11c7 -[NSURLConnectionInternal _withActiveConnectionAndDelegate:] + 48
23 CFNetwork 0x000000010aca0757 ___ZN27URLConnectionClient_Classic26_delegate_didFinishLoadingEU13block_pointerFvvE_block_invoke + 107
24 CFNetwork 0x000000010ad6dde1 ___ZN27URLConnectionClient_Classic18_withDelegateAsyncEPKcU13block_pointerFvP16_CFURLConnectionPK33CFURLConnectionClientCurrent_VMaxE_block_invoke_2 + 273
25 CFNetwork 0x000000010ac8ba26 _ZN19RunloopBlockContext13_invoke_blockEPKvPv + 72
26 CoreFoundation 0x000000010bb7c354 CFArrayApplyFunction + 68
27 CFNetwork 0x000000010ac8b8e7 _ZN19RunloopBlockContext7performEv + 133
28 CFNetwork 0x000000010ac8b726 _ZN17MultiplexerSource7performEv + 256
29 CFNetwork 0x000000010ac8b53c _ZN17MultiplexerSource8_performEPv + 72
30 CoreFoundation 0x000000010bba9431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
31 CoreFoundation 0x000000010bb9f2fd __CFRunLoopDoSources0 + 269
32 CoreFoundation 0x000000010bb9e934 __CFRunLoopRun + 868
33 CoreFoundation 0x000000010bb9e366 CFRunLoopRunSpecific + 470
34 GraphicsServices 0x000000010d3c5a3e GSEventRunModal + 161
35 UIKit 0x000000010c06e900 UIApplicationMain + 1282
36 Hang 0x0000000109e39acf main + 111
37 libdyld.dylib 0x0000000110e59145 start + 1
38 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
Why is this happening? How can I set my UIImageView from the Facebook Graph Request result.
try this code:
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:#{#"fields": #"id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location , friends ,hometown , friendlists"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error)
{
NSLog(#"result is:%#",result);
yourimageView.image = [UIImage imageWithContentsOfURL:[NSURL URLWithString:[[[result objectForKey:#"picture"] objectForKey:#"data"] objectForKey:#"url"]]];
}
else
{
NSLog(#"Error %#",error);
}
}];

Sort NSMutableArray by two values

Earlier I was sorting NSMutableArray in below way.
NSSortDescriptor *sortDescriptor1;
sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:#"Carton_IsDelivered"
ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor1];
customerDetailsArray = [[customerDetailsArray sortedArrayUsingDescriptors:sortDescriptors] mutableCopy];
This was sorting based on Carton_IsDelivered value which hold true & false value.
This was working fine.
Now as per new requirement, I want to sort by Carton_IsDelivered & Carton_AreaName.
What I want is
Carton_IsDelivered=true group by Carton_AreaName
+Carton_IsDelivered=false group by Carton_AreaName
For that I used below.
NSSortDescriptor *sortDescriptor1;
NSSortDescriptor *sortDescriptor2;
sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:#"Carton_IsDelivered"
ascending:NO];
sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:#"Carton_AreaName"
ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObject:#[sortDescriptor1, sortDescriptor2]];
customerDetailsArray = [[customerDetailsArray sortedArrayUsingDescriptors:sortDescriptors] mutableCopy];
However the app crash saying below.
2015-03-30 11:35:45.402 Delivery[74822:9478073] -[__NSArrayI ascending]: unrecognized selector sent to instance 0x7ca5a660
2015-03-30 11:35:45.433 Delivery[74822:9478073] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI ascending]: unrecognized selector sent to instance 0x7ca5a660'
*** First throw call stack:
(
0 CoreFoundation 0x01f6f466 __exceptionPreprocess + 182
1 libobjc.A.dylib 0x01bf8a97 objc_exception_throw + 44
2 CoreFoundation 0x01f772c5 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
3 CoreFoundation 0x01ebfbc7 ___forwarding___ + 1047
4 CoreFoundation 0x01ebf78e _CF_forwarding_prep_0 + 14
5 Foundation 0x017d611e _NSInitializeCacheLine + 60
6 Foundation 0x017d5ee0 _sortedObjectsUsingDescriptors + 293
7 Foundation 0x017d5d51 -[NSArray(NSKeyValueSorting) sortedArrayUsingDescriptors:] + 713
8 Delivery 0x000d4388 -[DeliveryViewController bringDeliveredToTop] + 392
9 Delivery 0x000d0b4b -[DeliveryViewController parserDidEndDocument:] + 875
10 Foundation 0x018b1c34 _endDocument + 89
11 libxml2.2.dylib 0x057b6150 xmlParseChunk + 1366
12 Foundation 0x018afbf4 -[NSXMLParser finishIncrementalParse] + 58
13 Foundation 0x018afd95 -[NSXMLParser parseFromStream] + 385
14 Foundation 0x018afea8 -[NSXMLParser parse] + 33
15 Delivery 0x000cdfdc -[DeliveryViewController connectionDidFinishLoading:] + 1836
16 CFNetwork 0x05a1df39 ___ZL32_NSURLConnectionDidFinishLoadingP16_CFURLConnectionPKv_block_invoke + 40
17 CFNetwork 0x05a35af9 __65-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]_block_invoke + 83
18 CFNetwork 0x05a1cfe9 -[NSURLConnectionInternalConnection invokeForDelegate:] + 145
19 CFNetwork 0x05a35a93 -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] + 189
20 CFNetwork 0x05a35c46 -[NSURLConnectionInternal _withActiveConnectionAndDelegate:] + 58
21 CFNetwork 0x05a1c5e0 _ZL32_NSURLConnectionDidFinishLoadingP16_CFURLConnectionPKv + 43
22 CFNetwork 0x058e5fc5 ___ZN27URLConnectionClient_Classic26_delegate_didFinishLoadingEU13block_pointerFvvE_block_invoke + 115
23 CFNetwork 0x059c59ad ___ZN27URLConnectionClient_Classic18_withDelegateAsyncEPKcU13block_pointerFvP16_CFURLConnectionPK33CFURLConnectionClientCurrent_VMaxE_block_invoke_2 + 290
24 CFNetwork 0x058d064c _ZN19RunloopBlockContext13_invoke_blockEPKvPv + 70
25 CoreFoundation 0x01e63db9 CFArrayApplyFunction + 57
26 CFNetwork 0x058d050d _ZN19RunloopBlockContext7performEv + 149
27 CFNetwork 0x05a26fd6 _ZThn16_N19RunloopBlockContext24multiplexerClientPerformEv + 20
28 CFNetwork 0x058d0350 _ZN17MultiplexerSource7performEv + 292
29 CFNetwork 0x058d016c _ZN17MultiplexerSource8_performEPv + 76
30 CoreFoundation 0x01e9298f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
31 CoreFoundation 0x01e8849d __CFRunLoopDoSources0 + 253
32 CoreFoundation 0x01e879f8 __CFRunLoopRun + 952
33 CoreFoundation 0x01e8737b CFRunLoopRunSpecific + 443
34 CoreFoundation 0x01e871ab CFRunLoopRunInMode + 123
35 GraphicsServices 0x0424d2c1 GSEventRunModal + 192
36 GraphicsServices 0x0424d0fe GSEventRun + 104
37 UIKit 0x005be9b6 UIApplicationMain + 1526
38 Delivery 0x000b4882 main + 130
39 libdyld.dylib 0x04772ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Any idea what I am doing wrong?
As far as I see you use arrayWithObject which takes only one parameter, and you pass array so you have array which contains another array, you need to have just one array with two objects, try this instead:
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor1, sortDescriptor2, nil];
Change the arrayWithObject: to arrayWithObjects:
NSArray *sortDescriptors = [NSArray arrayWithObjects:#[sortDescriptor1, sortDescriptor2]];

uploading NSData using AFNetwork terminating my APP

I'm trying to upload a NSData which is image using AFNetwork but something going wrong which i cannot find where is my mistake
here is my code:
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:#"test.jpg"], 0.1);
NSMutableDictionary *postParams = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"this is only a test post for \"app\".", #"app",
#"this POST for \"test\". ", #"test", nil];
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:#"POST" URLString:#"http://url/upload.php" parameters:postParm constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:#"file" fileName:#"filename.jpg" mimeType:#"image/jpeg" ];
} error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSProgress *progress = nil;
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(#"Error: %#", error);
} else {
NSLog(#"%# %#", response, responseObject);
}
}];
[uploadTask resume];
and the crash result is this:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -streamStatus only defined for abstract class. Define -[AFMultipartBodyStream streamStatus]!'
* First throw call stack:
(
0 CoreFoundation 0x019d91e4 exceptionPreprocess + 180
1 libobjc.A.dylib 0x017588e5 objc_exception_throw + 44
2 CoreFoundation 0x019d8fbb +[NSException raise:format:] + 139
3 Foundation 0x01433b3b NSRequestConcreteImplementation + 218
4 Foundation 0x0143b746 -[NSStream(NSStream) streamStatus] + 48
5 readWritePlist 0x000333a1 -[AFMultipartBodyStream open] + 49
6 CoreFoundation 0x0199ac44 CFReadStreamOpen + 116
7 CFNetwork 0x026f395d _ZN30CoreReadStreamFromCFReadStream16_streamImpl_OpenEP13CFStreamErrorPh + 53
8 CFNetwork 0x027c5ae6 _ZThn60_N30CoreReadStreamFromCFReadStream16_streamImpl_OpenEP13CFStreamErrorPh + 34
9 CFNetwork 0x026d1112 _ZN14CoreStreamBase21_streamInterface_OpenEv + 72
10 CFNetwork 0x026f2c34 _ZN17HTTPNetConnection19prepareTransmissionEP17HTTPNetStreamInfoP17__CoreWriteStream + 1614
11 CFNetwork 0x026f2063 _ZN17HTTPNetConnection19requestStateChangedEPvN13NetConnection5StateEP13CFStreamError + 415
12 CFNetwork 0x026f21fd _ZN13NetConnection18scheduleNewRequestEP13_CFNetRequestS1_h + 293
13 CFNetwork 0x026f1da4 _ZN13NetConnection7enqueueEPvh + 538
14 CFNetwork 0x026f1b26 _ZN17HTTPNetStreamInfo16_streamImpl_OpenEP13CFStreamErrorPh + 104
15 CFNetwork 0x02756846 _ZThn60_N17HTTPNetStreamInfo16_streamImpl_OpenEP13CFStreamErrorPh + 34
16 CFNetwork 0x026d1112 _ZN14CoreStreamBase21_streamInterface_OpenEv + 72
17 CFNetwork 0x026f1315 _ZN12HTTPProtocol10openStreamEv + 267
18 CFNetwork 0x0276498c _ZN12HTTPProtocol26useNetConnectionForRequestEP13NetConnectionP15__CFHTTPMessageh + 1550
19 CFNetwork 0x026f05d8 _ZN24HTTPConnectionCacheEntry28dispatchConnectionToProtocolEP13NetConnectionP12HTTPProtocolP18HTTPRequestMessageh + 284
20 CFNetwork 0x026f03b9 _ZN24HTTPConnectionCacheEntry34notifyNextProtocolOfOpenConnectionEP13NetConnectionh + 349
21 CFNetwork 0x026ed71d _ZN24HTTPConnectionCacheEntry25enqueueRequestForProtocolEP12HTTPProtocolP15__CFHTTPMessage + 637
22 CFNetwork 0x026ed0db _ZN19HTTPConnectionCache34_onqueue_enqueueRequestForProtocolEP12HTTPProtocolP15__CFHTTPMessage + 185
23 CFNetwork 0x0276a897 ___ZN19HTTPConnectionCache25enqueueRequestForProtocolEP12HTTPProtocolP15__CFHTTPMessage_block_invoke + 35
24 CoreFoundation 0x0197ac69 CFArrayApplyFunction + 57
25 CFNetwork 0x026e8441 _ZN19RunloopBlockContext7performEv + 155
26 CFNetwork 0x027ca3f4 _ZThn16_N19RunloopBlockContext24multiplexerClientPerformEv + 20
27 CFNetwork 0x026e8257 _ZN17MultiplexerSource7performEv + 299
28 CFNetwork 0x026e806c _ZN17MultiplexerSource8_performEPv + 76
29 CoreFoundation 0x0196277f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 15
30 CoreFoundation 0x0196210b CFRunLoopDoSources0 + 235
31 CoreFoundation 0x0197f1ae __CFRunLoopRun + 910
32 CoreFoundation 0x0197e9d3 CFRunLoopRunSpecific + 467
33 CoreFoundation 0x0197e7eb CFRunLoopRunInMode + 123
34 Foundation 0x01353ece +[NSURLConnection(Loader) _resourceLoadLoop:] + 381
35 Foundation 0x013afa07 -[NSThread main] + 76
36 Foundation 0x013af966 __NSThread__main + 1275
37 libsystem_pthread.dylib 0x0241d5fb _pthread_body + 144
38 libsystem_pthread.dylib 0x0241d485 _pthread_struct_init + 0
39 libsystem_pthread.dylib 0x02422cf2 thread_start + 34
)
libc++abi.dylib: terminating with uncaught exception of type NSException
any idea what is my mistake here..!?
After I update AFNetwork to latest version which is 2.3.1, problem got solved.

when i enter first character in search bar my app crashes

I have parsed JSON in tableview and I want to implement search bar to filter huge data. I have tried implementing search bar from here.
but it crashes.
-(void)parsingmethod{
NSString *string = [NSString stringWithFormat:#"HEREISMYJSONURL"];
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
self.json=[responseObject valueForKey:#"name"];
self.filteredCandyArray = [NSMutableArray arrayWithCapacity:[candyArray count]];
self.filteredArray=[NSMutableArray arrayWithArray:self.json];
NSLog(#"self.flteredarray=%#",self.filteredArray);
self.current_address = [responseObject valueForKey:#"current_address"];
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(#"Oops, something went wrong: %#", [error localizedDescription]);
}];
[operation start];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if ( cell == nil ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text =[self.filteredArray objectAtIndex:indexPath.row];
} else {
cell.textLabel.text =[self.json objectAtIndex:indexPath.row];
}
cell.detailTextLabel.text=[self.current_address objectAtIndex:indexPath.row];
return cell;
}
And content filtering and delegate method also done from here
Below my Crash Logs
***** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0xcc515f0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key name.'
*** First throw call stack:
(
0 CoreFoundation 0x021af1e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x0199c8e5 objc_exception_throw + 44
2 CoreFoundation 0x0223efe1 -[NSException raise] + 17
3 Foundation 0x0165cc7a -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 282
4 Foundation 0x015c9dfd _NSGetUsingKeyValueGetter + 81
5 Foundation 0x015c9437 -[NSObject(NSKeyValueCoding) valueForKey:] + 260
6 Foundation 0x0159a465 -[NSFunctionExpression expressionValueWithObject:context:] + 1260
7 Foundation 0x016202c1 -[NSComparisonPredicate evaluateWithObject:substitutionVariables:] + 248
8 Foundation 0x016201c1 -[NSPredicate evaluateWithObject:] + 48
9 Foundation 0x01620141 _filterObjectsUsingPredicate + 418
10 Foundation 0x0161fefa -[NSArray(NSPredicateSupport) filteredArrayUsingPredicate:] + 328
11 LuckyHR 0x00043995 -[NameListTableViewController filterContentForSearchText:scope:] + 245
12 LuckyHR 0x00043bf2 -[NameListTableViewController searchDisplayController:shouldReloadTableForSearchString:] + 386
13 UIKit 0x00a1eb37 -[UISearchDisplayController searchBar:textDidChange:] + 128
14 UIKit 0x0093dc56 -[UISearchBar(UISearchBarStatic) _searchFieldEditingChanged] + 178
15 libobjc.A.dylib 0x019ae82b -[NSObject performSelector:withObject:] + 70
16 UIKit 0x0065e3b9 -[UIApplication sendAction:to:from:forEvent:] + 108
17 UIKit 0x0065e345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
18 UIKit 0x0075fbd1 -[UIControl sendAction:to:forEvent:] + 66
19 UIKit 0x0075ffc6 -[UIControl _sendActionsForEvents:withEvent:] + 577
20 UIKit 0x00d8529d -[UITextField fieldEditorDidChange:] + 221
21 UIKit 0x00765db4 -[UIFieldEditor textInputDidChange:] + 58
22 UIKit 0x00d931b3 -[UITextInputController _sendDelegateChangeNotificationsForText:selection:] + 118
23 UIKit 0x00d95090 -[UITextInputController _insertText:fromKeyboard:] + 768
24 UIKit 0x00d95954 -[UITextInputController insertText:] + 372
25 UIKit 0x00768a64 -[UIFieldEditor insertText:] + 1086
26 UIKit 0x00d88ffa -[UITextField insertText:] + 59
27 UIKit 0x00848760 -[UIKeyboardImpl insertText:] + 87
28 UIKit 0x00859ea4 -[TIKeyboardOperationInsertText(UIKeyboardImpl) main] + 83
29 Foundation 0x01670c79 -[__NSOperationInternal _start:] + 671
30 Foundation 0x015ed9c8 -[NSOperation start] + 83
31 UIKit 0x00846d4d -[UIKeyboardImpl performOperations:] + 153
32 UIKit 0x00844f8e -[UIKeyboardImpl continueHandleKeyboardInputWithOperations:] + 75
33 UIKit 0x00844ddc __73-[UIKeyboardImpl replyHandlerForHandleKeyboardInputWithExecutionContext:]_block_invoke_2 + 44
34 UIKit 0x00dae978 -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 402
35 libobjc.A.dylib 0x019ae82b -[NSObject performSelector:withObject:] + 70
36 Foundation 0x015f0e48 __NSThreadPerformPerform + 285
37 CoreFoundation 0x0213877f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
38 CoreFoundation 0x0213810b __CFRunLoopDoSources0 + 235
39 CoreFoundation 0x021551ae __CFRunLoopRun + 910
40 CoreFoundation 0x021549d3 CFRunLoopRunSpecific + 467
41 CoreFoundation 0x021547eb CFRunLoopRunInMode + 123
42 GraphicsServices 0x031d45ee GSEventRunModal + 192
43 GraphicsServices 0x031d442b GSEventRun + 104
44 UIKit 0x0065cf9b UIApplicationMain + 1225
45 LuckyHR 0x000499fd main + 141
46 libdyld.dylib 0x0392a70d start + 1
47 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) *
I went to similar questions posted here but still not figured it out.
It's clearly written :
3 Foundation 0x0165cc7a -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 282
4 Foundation 0x015c9dfd _NSGetUsingKeyValueGetter + 81
5 Foundation 0x015c9437 -[NSObject(NSKeyValueCoding) valueForKey:] + 260
You ask for a key that doesn't exist so it's not working.

Add empty object to an array

I need to add an empty object to index 0 and index 1 of an array being populated with data from a third party XML feed.
This is my parseXML method, it works.
-(void) parseXML{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"APIKEYHERECANTSHOW YOU"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *xmlString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSLog(#"The string : %#", xmlString);
NSDictionary *xml = [NSDictionary dictionaryWithXMLString:xmlString];
NSLog(#"The dict:%#", xml);
NSMutableDictionary *PageItem = [xml objectForKey:#"TeamLeagueStanding"];
NSLog(#"PageItem: %#", PageItem);
NSMutableArray *items = [xml objectForKey:#"TeamLeagueStanding"];
NSNull *nullValue = [NSNull null];
[items insertObject:nullValue atIndex:0]; <- THIS MAKES MY APP CRASH
NSLog(#"The array: %#", items);
[self setTableData:items];
}
But when i run this i get a crash with the console output:
2014-02-03 21:24:09.063 Liga Zon Sagres Companion[9645:70b] -[NSNull objectForKey:]: unrecognized selector sent to instance 0x101a85b40
2014-02-03 21:24:09.066 Liga Zon Sagres Companion[9645:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull objectForKey:]: unrecognized selector sent to instance 0x101a85b40'
*** First throw call stack:
(
0 CoreFoundation 0x000000010192a795 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010168d991 objc_exception_throw + 43
2 CoreFoundation 0x00000001019bbbad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010191c09d ___forwarding___ + 973
4 CoreFoundation 0x000000010191bc48 _CF_forwarding_prep_0 + 120
5 Liga Zon Sagres Companion 0x000000010000ee20 -[StandingsTableViewController tableView:cellForRowAtIndexPath:] + 256
6 UIKit 0x00000001003bbb8a -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 348
7 UIKit 0x00000001003a3836 -[UITableView _updateVisibleCellsNow:] + 2297
8 UIKit 0x00000001003b4381 -[UITableView layoutSubviews] + 207
9 UIKit 0x000000010034bb27 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 354
10 QuartzCore 0x0000000102081a22 -[CALayer layoutSublayers] + 151
11 QuartzCore 0x0000000102076589 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 363
12 QuartzCore 0x0000000102081956 -[CALayer layoutIfNeeded] + 162
13 UIKit 0x00000001003ebfc2 -[UIViewController window:setupWithInterfaceOrientation:] + 264
14 UIKit 0x000000010032ab4d -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 4360
15 UIKit 0x0000000100329a3f -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 36
16 UIKit 0x000000010032998f -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 101
17 UIKit 0x0000000100328c9e -[UIWindow _updateToInterfaceOrientation:duration:force:] + 377
18 UIKit 0x00000001003dfd4a -[UIViewController _tryBecomeRootViewControllerInWindow:] + 147
19 UIKit 0x0000000100323a87 -[UIWindow addRootViewControllerViewIfPossible] + 506
20 UIKit 0x0000000100323bd5 -[UIWindow _setHidden:forced:] + 275
21 UIKit 0x000000010032cca2 -[UIWindow makeKeyAndVisible] + 51
22 UIKit 0x00000001002eb0c8 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1449
23 UIKit 0x00000001002eebe8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 660
24 UIKit 0x00000001002ffaab -[UIApplication handleEvent:withNewEvent:] + 3092
25 UIKit 0x00000001002fff1e -[UIApplication sendEvent:] + 79
26 UIKit 0x00000001002f02be _UIApplicationHandleEvent + 618
27 GraphicsServices 0x0000000102578bb6 _PurpleEventCallback + 762
28 GraphicsServices 0x000000010257867d PurpleEventCallback + 35
29 CoreFoundation 0x00000001018ac819 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
30 CoreFoundation 0x00000001018ac5ee __CFRunLoopDoSource1 + 478
31 CoreFoundation 0x00000001018d5ab3 __CFRunLoopRun + 1939
32 CoreFoundation 0x00000001018d4f33 CFRunLoopRunSpecific + 467
33 UIKit 0x00000001002ee4bd -[UIApplication _run] + 609
34 UIKit 0x00000001002f0043 UIApplicationMain + 1010
35 Liga Zon Sagres Companion 0x0000000100011f93 main + 115
36 libdyld.dylib 0x0000000102f815fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Any ideas how to solve this? Thank you.
The error is coming from your StandingsTableViewController tableView:cellForRowAtIndexPath: method. Your data is giving you an NSNull instance where you expect an NSDictionary.
Since you explicitly add the NSNull object you need to update your cellForRow... method to check to see if the object is an NSNull instance before assuming it is an NSDictionary.
Something like this:
NSDictionary *data = self.tableData[someIndex];
if ([data isKindOfClass:[NSDictionary class]]) {
// process the data as usual
} else {
// This is probably the NSNull object, ignore it or handle appropriately
}

Resources