OK, thanks to your hint with the exception breakpoint I was able to read the error variable: Surprisingly it's nil. This caused me getting entirely confused. 😕 Anyway, in the console there is the NSException error printed
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
as well as a bunch of throw call stack annotations. I have already browsed them, but they didn't gimme a hint about the occured error. Clueless as I am, I assume that your red herring theory proves to be true. Possibly I forgot to set some delegates ... Would be absolutely cool if you have a look at the whole method. Maybe everything gets clearer, then.
- (void)downloadItemsWithQuery:(NSString *)searchString {
// Create the customized URL
NSString *theQuery = [searchString uppercaseString];
NSString *encodedString = [theQuery stringByAddingPercentEncodingWithAllowedCharacters:
[NSCharacterSet characterSetWithCharactersInString:@"!*'();:@&=+$,/?%#[]"]];
NSURL *theURL = [NSURL URLWithString:[NSString stringWithFormat:
@"http://alexpoets.com/VPlan/service.php?klasse=%@", encodedString]];
// Create the session to fetch the JSON
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:theURL
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
// handle response
if (!error) {
// Initialize the data object
_downloadedData = [[NSMutableData alloc] init];
// Append the newly downloaded data
[_downloadedData appendData:data];
} else NSLog(@"%@", error);
}] resume];
// Create an array to store the data
NSMutableArray *_storedData = [[NSMutableArray alloc] init];
// Parse the JSON that came in
NSError *error;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:_downloadedData
options:NSJSONReadingAllowFragments error:&error];
// Loop through JSON data, create new objects and add them to the array
for (int i = 0; i < jsonArray.count; i++)
{
NSDictionary *jsonElement = jsonArray[i];
// Create new objects and set its props to JsonElement properties
Qualifikationsphase *newQualifikationsphase = [[Qualifikationsphase alloc] init];
newQualifikationsphase.tag = jsonElement[@"Tag"];
newQualifikationsphase.art = jsonElement[@"Art"];
newQualifikationsphase.klasse = jsonElement[@"Klasse"];
newQualifikationsphase.stunde = jsonElement[@"Stunde"];
newQualifikationsphase.vertretung = jsonElement[@"Vertretung"];
newQualifikationsphase.fach = jsonElement[@"Fach"];
newQualifikationsphase.raum = jsonElement[@"Raum"];
newQualifikationsphase.bemerkung = jsonElement[@"Bemerkung"];
newQualifikationsphase.identifier = jsonElement[@"Identifier"];
// Add objects to the array
[_storedData addObject:newQualifikationsphase];
}
// Ready to notify delegate that data is ready and pass back items
if (self.delegate) [self.delegate itemsDownloaded:_storedData];
}
I'm so sorry for this bunch of code!!! 😊 It's my model (MVC pattern), BTW. I call this method in the controller, before the view is loaded. In the header file of my model class I set the following delegate:
#import <Foundation/Foundation.h>
@protocol HomeModelProtocol <NSObject>
- (void)itemsDownloaded:(NSArray *)items;
@end
@interface HomeModel : NSObject <NSURLSessionDataDelegate>
@property (nonatomic, weak) id<HomeModelProtocol>
delegate;
- (void)downloadItemsWithQuery:(NSString *)searchString;
@end
When the delegate is notified that the data items are ready to get passed back, this method in my controller is called:
- (void)itemsDownloaded:(NSArray *)items {
// This delegate method will get called when the items are finished downloading
// Set the downloaded items to the array
_feedItems = items;
// Reload the table view
[self.listTableView reloadData];
}
Possibly I made a mistake concerning the delegates or rather the MVC pattern. I am SO grateful for any help you can provide!!! 🙂
Kind regards,
Alex