I have a DiningHall
class that has the 3 properties: openingTime
, closingTime
, and nextMealOpeningTime
, and I need to set these NSDate
properties from the data from this website.
Each DiningHall
object corresponds to a particular meal. For example, there is a separate "De Neve" dining hall object for breakfast, lunch, and dinner.
As an example, if I have a De Neve hall object corresponding to "Lunch", and the times were
breakfast (9-11) lunch (12-2) dinner (5-8)
then openingTime
is 12pm, closingTime
is 2pm, and nextMealOpeningTime
is 5pm.
The general algorithm is:
given a particular meal: for each hall in data, find row corresponding to hall find column corresponding to current meal parse text, convert toNSDate pass 3 NSDate's to DiningHall as array
I'm using an open source DOM parser, and below is the code for extracting the text once I obtain the correct row. Specific concerns are dealing with "Closed" meals and a better way to pass data to DiningHall
class, but any optimizations will help.
- (NSArray*) getHourDataForRow:(TFHppleElement*)row Meal:(NSString*)meal {
NSArray *meals = [row childrenWithTagName:@"td"];
NSArray *mealNames = [NSArray arrayWithObjects:@"breakfast", @"lunch", @"dinner", nil];
int index = [mealNames indexOfObject:meal]+1;
TFHppleElement *cell = meals[index];
NSArray *strong = [cell childrenWithTagName:@"strong"];
NSMutableArray *retArr = [NSMutableArray array];
NSString *opening = [strong[0] firstChildWithTagName:@"text"].content;
NSDate *openingAsDate = [self dateFromString:opening];
if (openingAsDate){
[retArr addObject:openingAsDate];
NSString *closing = [strong[1] firstChildWithTagName:@"text"].content;
[retArr addObject:[self dateFromString:closing]];
}
else{
[retArr addObject:[NSNull null]];
[retArr addObject:[NSNull null]];
}
if (index +1 <= 3) {
TFHppleElement *nextCell = meals[index+1];
NSArray *strong1 = [nextCell childrenWithTagName:@"strong"];
NSString* nextOpening = [strong1[0] firstChildWithTagName:@"text"].content; //strong[0] is opening time, don't need closing time from next cell
[retArr addObject:[self nextOpening.content]];
}
else // nextMealOpeningTime is always nil for dinner
[retArr addObject:[NSNull null]];
return retArr;
}
- (NSDate*) dateFromString:(NSString*)time {
if ([time isEqualToString:@"CLOSED"])
return nil;
NSArray *digits = [time componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
int hour = [digits[0] intValue];
hour+= (([time rangeOfString:@"pm"].location == NSNotFound) ? 0:12) ; //add 12 for pm
int minutes = [digits[1] intValue];
//format nsdate
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit ) fromDate:[NSDate date]];
[components setHour:hour];
[components setMinute:minutes];
return [calendar dateFromComponents:components];
}
In DiningHall class
- (void) setHoursFromData:(NSArray*)data {
if (data[0] != [NSNull null] && data[1] != [NSNull null]){
_openingTime = data[0];
_closingTime = data[1];
}
if (data[2] != [NSNull null])
_nextOpeningTime = data[2];
}
NSXMLParser
? \$\endgroup\$ – nhgrif Jul 24 '14 at 22:27XML
. I know you're trying to parseHTML
, but I thinkXML
parsing might work here.NSXMLParser
is definitely efficient, and might present the data in a way that makes plenty of sense. \$\endgroup\$ – nhgrif Jul 25 '14 at 3:09