Retreiving string from NSDictionary

Total Post:214


 1440  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:]


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

      Modified On Apr-10-2018 01:27:25 AM


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.