Skip to content

Instantly share code, notes, and snippets.

@sdbeng
Last active August 29, 2015 13:57
Show Gist options
  • Save sdbeng/9459358 to your computer and use it in GitHub Desktop.
Save sdbeng/9459358 to your computer and use it in GitHub Desktop.
iPhone app - Pull out a list of events (per day) -
#import "Event.h"
#import "AFNetworking.h"
@implementation Event
-(id)init {
self = [self initWithTitle:@"defaultTitle" initWithDetail:@"defaultDetail"];
return self;
}
//-(id)initWithJSON {
//}
- (id)initWithTitle:(NSString *)aTitle
initWithDetail:(NSString *)aDetail {
self = [super init];
if(self) {
self.title = aTitle;
self.detail = aDetail;
}
return self;
}
[
{
title: "Intro to Objective-C",
detail: "Objective-C projects - 8:00AM"
},
{
title: "Main Keynote",
detail: "Some bright hackers here - 9:00AM"
},
{
title: "Cloud 501",
detail: "Deploy apps to the Cloud - 10:00AM"
}
]
{
"resultsDay1": [
{
"title": "Registration(1st day)",
"detail": "Main Hallway"
},
{
"title": "Pre-Conference",
"detail": "3 sessions starting at 12:00PM"
},
{
"title": "Opening Keynote",
"detail": "Beautiful sun @5txc"
}
],
"resultsDay2": [
{
"title": "Registration(2nd day)",
"detail": "Main Hallway"
},
{
"title": "event TBD",
"detail": "Silver room"
},
{
"title": "event TBD 2",
"detail": "Copper room"
}
]
}
#import <UIKit/UIKit.h>
#import "Event.h"
@interface ScheduleTableViewController : UITableViewController
@property(strong,nonatomic) Event *event;
@property (strong, nonatomic) NSArray *eventData;
@end
#import "ScheduleTableViewController.h"
#import "AFNetworking.h"
@interface ScheduleTableViewController ()
@end
@implementation ScheduleTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//self.event = [[Event alloc] initWithJSON];
// [[NSNotificationCenter defaultCenter]
// addObserver:self
// selector:@selector(dataRetrieved)
// name:@"initWithJSONFinishedLoading"
// object:nil];
NSURL *url = [[NSURL alloc] initWithString:@"http://backbone7.sdbapps.com/events2.json"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
for (NSDictionary *oneDictionary in responseObject) {
Event *newEvent = [[Event alloc] initWithTitle:responseObject[@"title"]
initWithDetail:responseObject[@"detail"]];
[tempArray addObject:newEvent];
NSLog(@"The NSMutable tempArray is: %@", tempArray);
NSLog(@"The new created event instance is: %@", newEvent);
}
self.eventData = [[NSArray alloc] initWithArray:tempArray];
NSLog(@"The final JSON array is: %@", self.eventData);
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Events!"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
}];
[operation start];
}
#pragma mark - Table view data source
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
Event *event = self.eventData [indexPath.row];
cell.textLabel.text = event.title;
cell.detailTextLabel.text = event.detail;
return cell;
}
@sdbeng
Copy link
Author

sdbeng commented Mar 10, 2014

Another concern I have is once the tableview is working with AFNetworking 2.0, would I be having issues adding push notifications? (I'm thinking Parse PNs). Since I need to add their API to my project. Last year I gave up on AFNet because I had a problem with both using json callbacks.

@kaspth
Copy link

kaspth commented Mar 10, 2014

I have created a fork with my changes.
They include both some stylistic changes and hopefully a fix in your parsing.

You should try thinking a little about your data structure, i.e. how you want to store events in memory.
Because your problem was caused by a mismatch in how your server structures events and how your client code parses it.

Let's compare the nesting in events2.json with your code.
events2.json: dictionary => array => dictionary
Your code: dictionary

Here's the gist:
https://gist.github.com/kaspth/9461284

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment