6

So I have an NSMutableDictionary that I populate, but when I output the contents to NSLog, the key and value for the entries entered in the for loop show up differently, and the final NSLog call doesn't even output anything. What's going on here??? Please help! Why are the quotes around the entries added in the for loop???

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                            numberOfPhotosAsString, @"PackageFileCount",
                            wtID, @"wtID",
                            uploadType, @"type",
                            nil];
    for (int i= 0; i < photos.count; i++)
    {
        NSString *finalFileName = [fileNameBase stringByAppendingFormat:@"%i", i];
        [params setObject:[[fileNames objectAtIndex:i] stringByAppendingString:@".jpg"] forKey:finalFileName];
        // This one doesn't do anything differently:
        //[params setValue:[[fileNames objectAtIndex:i] stringByAppendingString:@".jpg"] forKey:[fileNameBase stringByAppendingFormat:@"%i", i]];
    }
    NSLog(@"Params: %@", params);
    NSLog(@"Value: %@", [params objectForKey:@"SourceName_0"]);

Output of NSLog (I only added one value in the for loop: "SourceName_0" = "tbyg.jpg", but why the quotes around "SourceName_0"??? Whatever is happening here is preventing me from accessing that dictionary entry...

Log:

2012-05-09 12:38:26.448 PhotoUp[6231:707] Params: {
    PackageFileCount = 1;
    "SourceName_0" = "tbyg.jpg";
    type = T;
    wtID = "6bcb4126-4bbe-4b3d-be45-9a06cf56a22f";
}
2012-05-09 12:38:26.449 PhotoUp[6231:707] Value: (null)
HackyStack
  • 4,887
  • 3
  • 22
  • 28
  • What kind of object is `"SourceName_0"`? You can iterate over the keys and log the types by using `NSStringFromClass`. – Joe May 09 '12 at 16:59
  • They should both be strings, but type and wtID are both strings also, but don't show up in quotes.... I don't get it... The problem is I'm using these to post to a web form and when I process the web form, it doesn't find the "SourceName_x" one... Look at my last line of code, then look at the output from log for it... – HackyStack May 09 '12 at 17:08
  • The quotes appear because the string contains something besides basic alphanumerics — in this case, an underscore. I don't believe this is relevant to your problem. I can't reproduce it here by copying and pasting this code into a file (and filling in the undeclared variables with the values shown), so the problem must be something you're not showing us. – Chuck May 09 '12 at 17:09
  • 1
    Well the quotes are implementation defined in `NSDictionary`s `description` method. Based on my own personal observations you will generally only see quotes when there are spaces or symbols in the string. In your case an `_` in `SourceName_0`, `.` in `tbyg.jpg` and then a `-` in the `wtID`. – Joe May 09 '12 at 17:12
  • All the code is here. I'm passing the dictionary to a NSMutableURLRequest, but when the webpage process the request, it sees the value as nil for key "SourceName_x". I decided to output the contents of the dictionary using NSLog and it shows as being there (but with quotes), then when I try to output that one value using NSLog, it shows as nil... – HackyStack May 09 '12 at 17:12
  • I think the quotes around the key are the problem. The web form processes the wtid entry fine, but sees "SourceName_x" as nil... NSLog output shows it as nil too... – HackyStack May 09 '12 at 17:14
  • @HackyStack: Again, I copied and pasted your code verbatim into a program and ran it and got back "tbyg.jpg" in the second log. The problem is either in the contents of `fileNameBase` (which you haven't shown us how it's created, just like half the variables in this code sample) or in how your code is organized in your real program. This code sample *works* if you stick it in a new file and fill in all the variable values, so the problem is outside this code. – Chuck May 09 '12 at 17:17
  • I removed the underscore from the key, and that solves the issue, but how stupid! – HackyStack May 09 '12 at 17:25

1 Answers1

14

The quotes appear because the string contains something besides basic alphanumerics — in this case, an underscore. It's the same reason "tbyg.jpg" and "6bcb4126-4bbe-4b3d-be45-9a06cf56a22f" have quotes (they contain a dot and dashes, respectively). That's just how the description method works. It wouldn't cause your second log to fail.

Chuck
  • 234,037
  • 30
  • 302
  • 389
  • I'm referring to the quotes around the KEY not the value. I think the simple answer is you aren't allowed to have those characters in a key's name, since it obviously doesn't recognize it... I changed the name of the key from "SourceName_[x]" to "SourceName[x]" and it works fine... – HackyStack May 09 '12 at 17:35
  • @HackyStack: I know what you're referring to, and I don't know what to say but that you're mistaken. Here's a pastebin of your code with the variables filled in: http://pastebin.com/k15JRjkw — compile it with `cc -framework Foundation --std=c99` and you'll see the dictionary lookup works fine, underscore or no underscore. – Chuck May 09 '12 at 17:48
  • 1
    @Chuck Hey Chuck is there a way to remove these quotes then? I mean a generic way of removing it since some have the quotes and some dont. Is there a way to pass all of them through some function that removes the quotes if they exist and leaves it as it is if they dont have the quotes? – VarunMurali Aug 17 '14 at 10:44