objectForKey ничего не возвращает - PullRequest
0 голосов
/ 14 сентября 2011
-(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 для всего словаря также выводит ноль)

1 Ответ

1 голос
/ 14 сентября 2011

Поместите этот код в свой и посмотрите, что выводится для объекта 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;
}
...