Debugging classes returns nil


#1

I’ve jumped a chapter or two here and there, so I understand it’s probably my own fault.

I’ve been following the Chapter 28 code, and copied it letter for letter, however debugging:

Returns nil.

One thing that I noticed was that in ScheduleClass.h the @property for begin is set to nonatomic, strong yet when I tried that, I get errors about assigning etc. So for now, I set it to assign rather than strong or copy.

ScheduleFetcher.m looks like this:

[code]//
// ScheduleFetcher.m
// RanchForecast
//
// Created by James Brooks on 07/12/2011.
// Copyright 2011 Blue Bay Travel. All rights reserved.
//

#import “ScheduleFetcher.h”
#import “ScheduledClass.h”

@implementation ScheduleFetcher

  • (id)init
    {
    self = [super init];
    if (self) {
    classes = [[NSMutableArray alloc] init];
    dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@“yyyy-MM-dd HH:mm:ss zzzz”];
    }

    return self;
    }

  • (NSArray *)fetchClassesWithError:(NSError **)outError {
    BOOL success;
    NSURL *xmlURL = [NSURL URLWithString:@“http://bignerdranch.com/xml/schedule”];

    NSURLRequest *req = [NSURLRequest requestWithURL:xmlURL
    cachePolicy:NSURLRequestReturnCacheDataElseLoad
    timeoutInterval:30];

    NSURLResponse *resp = nil;

    NSData *data = [NSURLConnection sendSynchronousRequest:req returningResponse:&resp error];

    if(!data)
    return nil;

    [classes removeAllObjects];

    NSXMLParser *parser;
    parser = [[NSXMLParser alloc] initWithData:data];
    [parser setDelegate:self];

    success = [parser parse];
    if(!success) {
    *outError = [parser parserError];
    return nil;
    }

    NSArray *output = [classes copy];

    return output;
    }

#pragma mark -
#pragma mark NSXMLParserDelegate Methods

  • (void)parser:(NSXMLParser *)parser
    didStartElement:(NSString *)elementName
    namespaceURI:(NSString *)namespaceURI
    qualifiedName:(NSString *)qName
    attributes:(NSDictionary *)attributeDict {
    if([elementName isEqual:@“class”]) {
    currentFields = [[NSMutableDictionary alloc] init];
    }else if([elementName isEqual:@“offering”]) {
    [currentFields setObject:[attributeDict objectForKey:@“href”] forKey:@“href”];
    }
    }

  • (void)parser:(NSXMLParser *)parser
    didStartElement:(NSString *)elementName
    namespaceURI:(NSString *)namespaceURI
    qualifiedName:(NSString *)qName {
    if([elementName isEqual:@“class”]) {
    ScheduledClass *currentClass = [[ScheduledClass alloc] init];
    [currentClass setName:[currentFields objectForKey:@“offering”]];
    [currentClass setLocation:[currentFields objectForKey:@“location”]];
    [currentClass setHref:[currentFields objectForKey:@“href”]];

      NSString *beginString = [currentFields objectForKey:@"begin"];
      NSDate *beginDate = [dateFormatter dateFromString:beginString];
      
      [currentClass setBegin:beginDate];
      
      [classes addObject:currentFields];
      
      currentClass = nil;
      currentFields = nil;
    

    }else if(currentFields && currentString) {
    NSString *trimmed;
    trimmed = [currentString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    [currentFields setObject:trimmed forKey:elementName];
    }

    currentString = nil;
    }

-(void)parser:(NSXMLParser *)parser
foundCharacters:(NSString *)string {
if(!currentString) {
currentString = [[NSMutableString alloc] init];
}

[currentString appendString: string];

}

@end[/code]
I’ve connected my tableView dataSources etc, but alas, no data.

Any ideas?

P.S awesome book! I’m going to Jamaica on Saturday so I’ll be reading through it all over again.


#2

So the strong keyword problem was related to me needing to upgrade Xcode from 4.1.2 to 4.2.1 :slight_smile:

But still, no data in my table view.


#3

If classes is nil, that would suggest that -init isn’t being called. Can you put a breakpoint in -init and determine if that’s the case?


#4

Hmm, didn’t seem to breakpoint.

I don’t even get a log output anymore. I definitely did before I started with the interface. Back I go.


#5

How about in -applicationDidFinishLaunching:? Is the ScheduleFetcher being created there and so forth?