转义NSMutableDictionary中的字符

时间:2011-04-28 19:08:10

标签: iphone objective-c nsstring escaping nsmutabledictionary

当我将NSString作为NSMutableDictionary中的键的对象传递时,它似乎包含转义字符作为常规字符。我该如何删除它们?我已经尝试过stringByReplacingOccurrencesOfString:@“\”withString:@“”byt它不起作用。

这是我的代码和输出:

代码:

NSString* fql1 = [NSString stringWithFormat:@"SELECT page_id FROM place WHERE distance(latitude,longitude,\"%@\",\"%@\") < 1500 AND checkin_count > 5", latitude, longitude];
    NSString* fql2 = [NSString stringWithFormat:@"SELECT author_uid, post_id, timestamp, tagged_uids, message FROM checkin WHERE page_id IN (select page_id from #PlaceQuery)"];
    NSString* fql = [[NSString stringWithFormat:@"{\"PlaceQuery\":\"%@\",\"CheckInQuery\":\"%@\"}",fql1,fql2] stringByReplacingOccurrencesOfString:@"\\" withString:@""];


NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObject:fql forKey:@"queries"];

NSLog(@"%@", fql);
NSLog(@"%@", params);

NSLog的输出(@“%@”,fql):

  

{“PlaceQuery”:“SELECT page_id FROM place WHERE distance(纬度,经度,”37.331693“,” - 122.030457“)&lt; 1500 AND checkin_count&gt; 5”,“CheckInQuery”:“SELECT author_uid,post_id,timestamp ,tagged_uids,消息FROM checkin WHERE page_id IN(从#PlaceQuery中选择page_id)“}

NSLog的输出(@“%@”,params):

  

{       queries =“{\”PlaceQuery \“:\”SELECT page_id FROM place WHERE distance(纬度,经度,\“37.331693 \”,\“ - 122.030457 \”)&lt; 1500 AND checkin_count&gt; 5 \“,\”CheckInQuery \“:\”SELECT author_uid,post_id,timestamp,tagged_uids,message FROM checkin WHERE page_id IN(从#PlaceQuery中选择page_id)\“}”;   }

我一直在寻找解决方案的时间,所以非常感谢A LOT;)

2 个答案:

答案 0 :(得分:0)

NSLog在打印出来时转义字符串 - 你的字符串中可能实际上没有转义字符。

试试这个:

NSLog(@"%s\n", [ string UTF8String ] ) 

看看会发生什么......

答案 1 :(得分:0)

原来这是关于权限的问题......是的,尴尬...... 令我感到困惑的是转义输出中的转义字符......

谢谢@williham和@nielsbot