objectForKey 不返回任何内容

发布于 2024-12-04 10:54:56 字数 1075 浏览 0 评论 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 个对象和键。 但是:

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

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

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

Until there, everything is ok, the NSLog is showing that there are 2 objects and keys in the tempDict.
But then:

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

At the last else, where cError is 1, NSLog outputs nil for that object.
(NSLog for the entire dictionary also outputs nil)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感性 2024-12-11 10:54:56

将此代码放入您的代码中,看看 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;
}

Place this code in your and see what the output is for dict object...

-(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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文