objectForKey没有返回任何内容

时间:2011-09-13 23:08:11

标签: objective-c ios xcode nsmutabledictionary

-(NSMutableDictionary *)request:(NSString *)requestString {
    NSString *filepath = [[NSBundle mainBundle] pathForResource:requestString ofType:@"xml"];
    NSError *error = nil;
    respString = [NSString stringWithContentsOfFile:filepath usedEncoding:nil error:&error];
NSLog(@"Ping xml");
    tempDict = [[NSMutableDictionary alloc] initWithDictionary:[parser readXMLString:respString]];
    NSLog(@"%@",tempDict);
return tempDict;
}

直到那里,一切正常,NSLog显示tempDict中有2个对象和键。 但那时:

-(int) authenticateUser {
    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithDictionary:[self request:@"login"]];
    if ([[dictionary objectForKey:@"sessionId0"] isEqualToString:@"-1"] ) {
        //Some code
    }
    else {
        cError = 1;
        NSLog(@"%@",[dictionary objectForKey:@"sessionId0"]);
        appDelegate.sessionId = [dictionary objectForKey:@"sessionId0"];
    }
    [dictionary release];
    return cError;
}

在最后一个地方,cError为1,NSLog为该对象输出nil。 (整个字典的NSLog也输出nil)

1 个答案:

答案 0 :(得分:1)

将此代码放入您的代码中,看看dict对象的输出是什么......

-(int) authenticateUser {
NSDictionary* dict = (NSDictionary *)[self request:@"login"];
NSLog(@"Dictionary %@",dict);    

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithDictionary:[self request:@"login"]];
    if ([[dictionary objectForKey:@"sessionId0"] isEqualToString:@"-1"] ) {
        //Some code
    }
    else {
        cError = 1;
        NSLog(@"%@",[dictionary objectForKey:@"sessionId"]);
        appDelegate.sessionId = [dictionary objectForKey:@"sessionId"];
    }
    [dictionary release];
    return cError;
}