从NSDictionary中检索字符串

时间:2015-07-16 17:57:12

标签: ios objective-c

我正在处理以下功能:

-(void)logResults:(NSDictionary *)results {
    NSMutableString *logString = [[NSMutableString alloc]init];
    for (NSString *key in results) {
        if([key isEqualToString:@"index"]){
            NSString *string = [[NSString alloc]initWithString:[results objectForKey:key]];
            string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
            [results setValue:string forKey:key];
        }
        NSLog(@"KEY: %@, VALUE: %@", key, [results objectForKey:key]);
        [logString appendFormat:@"|%@ = %@ \n", key, [results objectForKey:key]];
    }
    self.logInfo.text = [logString stringByAppendingString:self.logInfo.text];
}

问题在于if,我收到以下错误:[_NSArrayM length] unrecognised sel...

我首先尝试使用以下表达式来处理if:

//[results setValue:[((NSString *)[results objectForKey:key]) stringByReplacingOccurrencesOfString:@"\n" withString:@""] forKey:@"index"];

导致:

  

由于未捕获的异常终止应用' NSInvalidArgumentException',原因:' - [__ NSArrayM stringByReplacingOccurrencesOfString:withString:]

1 个答案:

答案 0 :(得分:0)

您的[results objectForKey:key]];正在返回一个导致所有崩溃的数组。要从数组中创建NSString,请使用[results valueForKey:@"description"] componentsJoinedByString:@""];