添加双引号到NSString附加到变量

bas*_*ste 0 objective-c nsdictionary nsstring ios

可能重复:
如何使用双引号将NSString初始化为文本

如何在NSString中的变量中添加tan实际双引号以在NSDictionary中使用?例

NSString *ename=@"John Doe";
NSDictionary *profile=@{@"Profile":@{"Name":ename}};
Run Code Online (Sandbox Code Playgroud)

我需要显示如下"个人资料":{"姓名":"John Doe"}

Mar*_*n R 6

我需要显示如下"个人资料":{"姓名":"John Doe"}

你想创建JSON格式吗?然后你可以使用内置类NSJSONSerialization:

NSString *ename = @"John Doe";
NSDictionary *profile = @{@"Profile":@{@"Name":ename}};

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:profile options:0 error:NULL];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

NSLog(@"%@", jsonString);
Run Code Online (Sandbox Code Playgroud)

输出:

{"Profile":{"Name":"John Doe"}}
Run Code Online (Sandbox Code Playgroud)


Ano*_*dya 5

使用逃避字符\ ...

NSString *ename=@"This is double \" quote \"";
Run Code Online (Sandbox Code Playgroud)

如果要求在字典中添加引号,则必须添加一些特殊字符(非字母和非数字),然后键才会显示双引号.

[... description] simply wraps things in quotes for display that have non-alphanumeric characters in them.

并且调试器中的"abc"和abc之间没有区别.这仅用于dubugging,实际值始终为NSString @"abc".