我正在从远程服务器中拉取一个二维数组:
- (NSMutableArray*)qBlock{
NSURL *url = [NSURL URLWithString:@"http://wsome.php"];
NSError *error;
NSStringEncoding encoding;
NSString *response = [[NSString alloc] initWithContentsOfURL:url
usedEncoding:&encoding
error:&error];
const char *convert = [response UTF8String];
NSString *responseString = [NSString stringWithUTF8String:convert];
NSMutableArray *sample = [responseString JSONValue];
return sample;
}
并将它们放入:
NSMutableArray *qnBlock1 = [self qBlock];
NSString *answer1 = [NSString stringWithFormat:[[qnBlock1 objectAtIndex:0]objectAtIndex:1]];
answer = [[NSMutableDictionary alloc]init];
[answer setObject:answer1 forKey:@"1"];
question1.text = [[qnBlock1 objectAtIndex:0] objectAtIndex:0];
label1a.text = [[qnBlock1 objectAtIndex:0]objectAtIndex:2];
label1b.text = [[qnBlock1 objectAtIndex:0]objectAtIndex:3];
label1c.text = [[qnBlock1 objectAtIndex:0]objectAtIndex:4];
label1d.text = [[qnBlock1 objectAtIndex:0]objectAtIndex:5];
我在运行时收到这个错误
-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x6c179502012-04-30 09:43:50.794 AppName[371:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x6c17950'
这是因为二维数组的语法问题吗?
最佳答案
您返回的不是多维数组。您将返回一组 NSDictionary 对象。您得到的错误表明您正在尝试将消息 objectAtIndex:
发送到 NSDictionary 对象,但由于没有这样的选择器而失败。
更新:
在一旁闲聊后,发现以下内容属实:
- 用户使用 SBJson 库来解析他的 php 网络服务的返回值。
- 返回值是其中之一:
- 一个 NSDictionary,每个键是它在列表中(不基于索引)位置的文本表示(@“1”、@“2”等...),每个值都是 NSString 对象的 NSArray 、或
- NSString 对象的 NSArray(似乎是单个“答案”返回的方式)
这是我提供的让他遍历他的返回值的代码:
NSURL *url = [NSURL URLWithString:@"{his url}"];
NSError *error;
NSStringEncoding encoding;
NSString *response = [[NSString alloc] initWithContentsOfURL:url usedEncoding:&encoding error:&error];
const char *convert = [response UTF8String];
NSString *responseString = [NSString stringWithUTF8String:convert];
NSLog(@"%@", responseString);
SBJsonParser *parser = [[SBJsonParser alloc] init];
id sample = [parser objectWithString:responseString];
if ([sample isKindOfClass:[NSDictionary class]]) {
for (id item in sample) {
NSLog(@"%@", [item description]);
NSArray *subArray = [sample objectForKey:item]; // b/c I know it's an array
for (NSString *str in subArray) {
NSLog(@"item: %@", str);
}
}
} else if ([sample isKindOfClass:[NSArray class]]) {
for (NSString *str in sample) {
NSLog(@"item: %@", str);
}
}
希望对你有帮助,J!
关于objective-c - NSMutabel 二维数组不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10377639/