2011-09-13 107 views
0
-(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个对象和键。 但后来:objectForKey不返回任何东西

-(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该对象输出为零。 (NSLog的整个字典还输出零)

+0

确定'[自我要求:@“登录” ]'不是零?尝试NSLog它'authenticateUser' – Saphrosit

+0

只是扔在那里,但你确定你想分配一个新的字典的结果,而不是仅仅使用由[自我请求:]返回的字典?这不会帮助你的具体问题,只是为了你在这里做什么,我不明白你为什么要分配一个全新的字典并将请求的结果复制到它里面......对于你的实际问题,也许你忘了将这个request:方法添加到你的这个类的接口中? – wallacer

+0

你的if语句检查[dictionary objectForKey:@“sessionID0”],但是在你检查的其他部分[dictionary objectForKey:@“sessionID”]。这是你在你的实际代码中有一个输入错误吗? – sosborn

回答

1

将此代码放在你看看输出是什么字典对象......

-(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; 
}