0

I need to add double quotes to control_text value. However, even though the back slash works for strings, it doesn't work for NSDictionary values. Is there a way to handle the following situation?

NSDictionary *createText =  @{ @"text": @[ @{ @"type": @"control_text" , @"text": @"text" }]};

This is my log:

{
    text = (
        {
            text = text;
            type = "control_text";
        }
    );
 }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
user3606054
  • 591
  • 1
  • 5
  • 14
  • Post your error message. Backslash escaping quotes works just fine inside `NSString` literals. – Steve Madsen Oct 21 '14 at 01:51
  • Your syntax is fine. If you `NSLog` the dictionary then it will show \ before the " to indicate that there is are quotes in the string, but if you log the length of types' value you will get 14 - the length without the \ – Paulw11 Oct 21 '14 at 01:53
  • 3
    It seems that the quotes aren't really part of the content; the -description method simply wraps things in quotes for display that have non-alphanumeric characters in them. – carlodurso Oct 21 '14 at 02:05
  • 2
    Why do you want to add double quotes to the keys? They are fine the way they are. – rmaddy Oct 21 '14 at 02:26
  • 1
    I'm almost certain that you don't need any more quotes. Your dump above is how NSLog shows dictionarys and arrays. If you convert to JSON (which I suspect is the goal) the key values will be quoted, as will "text". – Hot Licks Oct 21 '14 at 03:24
  • possible duplicate of [NSMutableDictionary is adding quotes to keys and values - why?](http://stackoverflow.com/questions/2471398/nsmutabledictionary-is-adding-quotes-to-keys-and-values-why) – Hot Licks Oct 21 '14 at 03:46

2 Answers2

0

A NSDictionary keys can include quotes, they just need to be escaped:

NSDictionary *d = @{@"\"n\"" : @"v"};
NSLog(@"d: %@", d);
NSLog(@"n1: %@", d[@"\"n\""]);
NSLog(@"n2: %@", d[@"n"]);

Output:

d: {
    "\"n\"" = v;
}

n1: v

n2: (null)

But it really does not make sense in general to add quotes to the keys.

Don't be confused by NSLog(), it uses the description method which adds quotes and backslashes for clarity in the manner the Apple developers liked.

Swift:

let d1 = ["n" : "v"]      // ["n":"v"]
let d2 = ["\"n\"" : "v"]  // [""n"":"v"]
zaph
  • 111,848
  • 21
  • 189
  • 228
  • 1
    Actually, NSLog/description effectively *removes* quotes where they're unnecessary. String values are only quoted if they contain blanks or special characters. This goes for both keys and values. – Hot Licks Oct 21 '14 at 03:26
  • Yes, and while a nice touch that also causes confusion. Personally I would rather have consistency. Note that Swift always shows the quotes and in the case of a quoted key shows double quotes with no backslash. Nice. – zaph Oct 21 '14 at 03:38
  • Well, Objective-C's `description` format predates JSON, et al, by a dozen years or so, so it's hard to be real critical of it. You just gotta know what it means. – Hot Licks Oct 21 '14 at 03:42
0

What NSLog shows as:

{
    text = (
        {
            text = text;
            type = "control_text";
        }
    );
 }

when converted to JSON, will appear as:

{
    "text":[
        {
            "text":"text",
            "type":"control_text"
        }
    ]
 }

Assuming that your goal is to produce JSON, there is no need to add more quotes.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151