-1

I'm 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:]

Cœur
  • 37,241
  • 25
  • 195
  • 267
DevilInDisguise
  • 360
  • 1
  • 4
  • 14
  • `string` might be a `NSArray` hence the `[__NSArrayM stringByReplacingOccurrencesOfString:withString:]`. Check to see if `string` actually is getting the string that you want. – somtingwong Jul 16 '15 at 18:10
  • @user1813076 it is. the line: [logString appendFormat:@"|%@ = %@ \n", key, [results objectForKey:key]]; has been working fine. I am only doing this to trim a string i was already getting properly. btw, even the initial NSString *string = [[NSString alloc]initWithString:[results objectForKey:key]]; crashes, which i find very weird since the first-mentioned expression (in this comment) works, and they are both treating strings. I did also try to make a cast to NSString – DevilInDisguise Jul 16 '15 at 18:20
  • At what line exactly does it crash and what is the exact error message for the code block? – luk2302 Jul 16 '15 at 18:20
  • @DevilInDisguise please show your NSDictionary data, it problem about you are fetch wrong way data from it. – Pravin Tate Jul 16 '15 at 18:22
  • @luk2303 NSMutableString *string = [[NSMutableString alloc]initWithString:(NSString *)[results objectForKey:key]]; this initial line causes a crash. Referring to me above comment I think this is weird as a latter expression works. ERROR: erminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM length]: unrecognized selector sent to instance 0x17005ffb0' – DevilInDisguise Jul 16 '15 at 18:23
  • Add inside your if an additional if, something like `if ([[results objectForKey:key] isKindOfClass [NSString class]]) {}` and some NSLog into `else` statement to see if it's really NSString. – teamnorge Jul 16 '15 at 18:24
  • @teamnorge [logString appendFormat:@"|%@ = %@ \n", key, [results objectForKey:key]] would not execute if it wasn't a string. – DevilInDisguise Jul 16 '15 at 18:34
  • also `[logString appendFormat:@"|%@ = %@ \n", key, [results objectForKey:key]]` still executes if it wasn't a string. Short description why is [here](http://stackoverflow.com/questions/3396336/print-array-in-objective-c) – somtingwong Jul 16 '15 at 18:39
  • @DevilInDisguise, you could also check if your `[key isKindOfClass [NSString class]]` before comparing is it is equal to `@"index"`. – teamnorge Jul 16 '15 at 18:47
  • @user1813076 you are right. can i check the specific class without testing isMemberOfClass, conformsToProtocol on different types? – DevilInDisguise Jul 16 '15 at 18:52
  • @teamnorge the if returns 1 as it is – DevilInDisguise Jul 16 '15 at 18:52
  • @user1813076 u should post that as an answer, valueForKey or objectForKey both works(don't know the difference rly). Does it make sense that you are allowed to assign nsstring instances non-string types? this is what got me – DevilInDisguise Jul 16 '15 at 19:14
  • @DevilInDisguise one more thing I did not noticed initially, your `results` is of type `(NSDictionary *)` not `(NSMutableDictionary *)` and you attempt to setValue on non mutable object. – teamnorge Jul 16 '15 at 19:16
  • @teamnorge [setValue forKey:] is available for NSDictionary, at least pops up as suggestion in Xcode.. I got what i wanted another way now, but thx for your input. Amazing how someone just down voted myq uestion just now... ppl are funny lol. cheers – DevilInDisguise Jul 16 '15 at 21:51
  • why did i just get a down vote to this?......... – DevilInDisguise Aug 09 '15 at 15:37

1 Answers1

0

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

somtingwong
  • 361
  • 1
  • 6
  • 19
  • so it turned out that the json i was parsing into a string, thinking it was a string, I could just handle the objects and arrays instead of doing string analysing lol.... thx for the help! – DevilInDisguise Jul 16 '15 at 19:40