开发者

Crash Error Help NSString

I have some code that looks like this:

NSString *_parse = [NSString stringWithFormat:@"//%@",_user];
NSString *_status = [Parser parse:_parse:@"status"]; //parses the xml
if ([_status isEqualToString:@"1"]) { //error here
}

The application crashes when it checks if the strings are equal. However, this works fine when I replace the first line of code with this:

    NSString *_parse = [NSString stringWithFormat:@"//user1"];

But that does not use the "user" ivar which I need to be used because the value can be different. The ivar "user" is a NSString that is declared earlier with [self setUser:@"userX"]; What should I do to fix this? Thanks

EDIT: Here is the code that parses the xml:

+ (NSString *)dataFilePath:(BOOL)forSave {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *documentsPath = [documentsDirectory stringByAppendingPathComponent:@"hangout.xml"];
            return documentsPath;    
    }
    + (MyViewController *)parse:(NSString *)nodesForPath:(NSString *)elementsForName {

        NSString *filePath = [self dataFilePath:FALSE];
        NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:filePath];
        NSError *error;
        GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
        if (doc == nil) { return nil; }
        MyViewController *view = [[[MyViewController alloc] init] autorelease];
        NSArray *getVersionInfo = [doc nodesForXPath:nodesForPath error:nil];
        for (GDataX开发者_如何转开发MLElement *versionInfo in getVersionInfo) {
            NSArray *elm1 = [versionInfo elementsForName:elementsForName];
            GDataXMLElement *elm2 = (GDataXMLElement *) [elm1 objectAtIndex:0];
                return elm2.stringValue;
        }

        [doc release];
        [xmlData release];
        return view;
    }


The main problem is in your parse:nodesForPath:elementsForName: method. When you find the element you are looking for, it returns an NSString (inside the loop). When you don't find the element you are looking for, it exits the loop and returns an instance of MyViewController. Obviously you can't compare a view controller to a string, so that causes the crash.

This is inexplicable. Why would you ever do that? It's not used anywhere before you return it. It makes no sense to have that code in there. Cut the references to MyViewController out completely. Return nil from your parse:nodesForPath:elementsForName: method. Don't return prematurely within the loop without releasing your allocated memory or that memory will leak. There's another leak in your earlier return too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜