Last active
August 29, 2015 13:57
-
-
Save sdbeng/9459358 to your computer and use it in GitHub Desktop.
iPhone app - Pull out a list of events (per day) -
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
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" | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
#import "Event.h" | |
@interface ScheduleTableViewController : UITableViewController | |
@property(strong,nonatomic) Event *event; | |
@property (strong, nonatomic) NSArray *eventData; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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