RETREIVING STRING FROM NSDICTIONARY

Tarun Kumar

Total Post:214

Points:1642
Posted by  Tarun Kumar
 869  View(s)
Ratings:
Rate this:

Im working on the following function:

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

}

problem lies within the if, I get the following error: [_NSArrayM length] unrecognised sel...

I was first attempting to use following expression for the if's work:

//[results setValue:[((NSString *)[results objectForKey:key])

stringByReplacingOccurrencesOfString:@"\n" withString:@""] forKey:@"index"];

which resulted in:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM stringByReplacingOccurrencesOfString:withString:]

 

  1. Jayne Spooner

    Post:21

    Points:147
    Re: Retreiving string from NSDictionary

    Your [results objectForKey:key]];

    is returning an array which is causing all of your crashes.

    To create a NSString from the array use [results valueForKey:@"description"] componentsJoinedByString:@""];

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!