Home > DeveloperSection > Forums > Retreiving string from NSDictionary
Tarun Kumar

Total Post:210

Posted on    July-17-2015 1:57 AM

 iPhone iPhone  Ios  Objective C 

 1 Reply(s)
 562  View(s)
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:]


Jayne Spooner
Jayne Spooner

Total Post:21

Posted on    July-17-2015 9:06 AM

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:@""];

Don't want to miss updates? Please click the below button!

Follow MindStick