objectForKey 不返回任何内容
-(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将此代码放入您的代码中,看看 dict 对象的输出是什么......
Place this code in your and see what the output is for dict object...