我正在从 PLIST 中读取字符串数据,我用它来创建 JSON 字符串(顺便在 Facebook Connect 中使用)。
NSString *eventLink = [eventDictionary objectForKey:EVENT_FIND_OUT_MORE_KEY];
NSString *eventLinkEscaped = [eventLink stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *eventName = [eventDictionary objectForKey:EVENT_NAME_KEY];
NSString *eventDescription = [eventDictionary objectForKey:@"Description"];
NSString *eventImageAddress = [eventDictionary valueForKey:@"Image URL"];
if ([eventImageAddress length] == 0)
{
eventImageAddress = NO_EVENT_IMAGE_URL;
}
// Publish a story to the feed using the feed dialog
FBStreamDialog *facebookStreamDialog = [[[FBStreamDialog alloc] init] autorelease];
facebookStreamDialog.delegate = self;
facebookStreamDialog.userMessagePrompt = @"Publish to Facebook";
facebookStreamDialog.attachment =[NSString stringWithFormat: @"{\"name\":\"%@\",\"href\":\"%@\",\"description\":\"%@\",\"media\":[{\"type\":\"image\",\"src\":\"%@\",\"href\":\"%@\"}]}", eventName, eventLinkEscaped, eventDescription, eventImageAddress, eventLinkEscaped];
[facebookStreamDialog show];
所有这一切都很好,但某些事件描述(大约 150 个中的 4 个)出现在对话框中的文本是空白的。我找到了明显的候选者,即描述包含“例如”字符或版权符号。我的问题是,是否有一个简单的方法调用,例如 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding 可以确保任何狡猾的字符都被转义或忽略?
提前致谢,
戴夫