2011-09-13 143 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; 
}