iphone url 转义问题

发布于 2024-12-28 16:17:21 字数 825 浏览 1 评论 0 原文

嗨,我必须通过 JSON 将数据发送到服务器,通常我这样做:

  NSMutableString * temp=[[NSMutableString alloc] initWithString:service_registra_inc];
   //here I add more staff to temp
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:temp]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];

但我收到一些错误消息,表明 url 错误:类似于 url 长度,但我已经搜索过,这意味着我必须转义我的 url si 我发现这个功能对我不起作用:

 NSString *temp2=(__bridge_transfer NSString*)CFURLCreateStringByAddingPercentEscapes(NULL,((CFStringRef)temp.UTF8String),NULL,NULL,kCFStringEncodingUTF8);

程序就停止了,并显示signal EXC_BAD_ACCess

好吧,我真的不知道如何将可变字符串转换为 CFStringRef,所以 xcode 只是建议我进行更正,但我真的不明白发生了什么。请帮忙......我已阅读文档,但它没有说明如何将 NSSMutableString 转换为 CFStringRef 并返回,或者如何使用整个内容直接创建 NSURL 对象。 Thks

Hi I have to send data to server via JSON and usually I do it like that:

  NSMutableString * temp=[[NSMutableString alloc] initWithString:service_registra_inc];
   //here I add more staff to temp
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:temp]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];

but I get some error message that the url is wrong: something like url length but I have searched around and it means I have to escape my url si I have found this fonction that doesn't work for me:

 NSString *temp2=(__bridge_transfer NSString*)CFURLCreateStringByAddingPercentEscapes(NULL,((CFStringRef)temp.UTF8String),NULL,NULL,kCFStringEncodingUTF8);

and there the program just stops and it says signal EXC_BAD_ACCess.

Well I don't really know how to transform mutable strings into CFStringRef so xcode just suggested the corrections for me but I don't really understand what is happening. Please help.... I have read the doc but it doesn't say how to cast NSSMutableString to CFStringRef and back or how to use the whole thing to create an NSURL object directly. Thks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一瞬间的火花 2025-01-04 16:17:21

你为什么使用 CFURL & CFStringRef 函数在这里?

您可以通过 NSString 的 stringByAddingPercentEscapesUsingEncoding: 方法。我已经为您链接了文档。

像这样的东西:(

 NSMutableString * temp=[[NSMutableString alloc] initWithString:service_registra_inc];

 // append your staff... errr, stuff here.

 NSString * temp2 = [temp stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:temp2]];
 [NSURLConnection alloc] initWithRequest:request delegate:self];

如果你不使用 ARC,不要忘记释放东西)

Why are you using CFURL & CFStringRef functions here?

You could do what you are trying to do via NSString's stringByAddingPercentEscapesUsingEncoding: method. I've linked the documentation for you.

Something like:

 NSMutableString * temp=[[NSMutableString alloc] initWithString:service_registra_inc];

 // append your staff... errr, stuff here.

 NSString * temp2 = [temp stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:temp2]];
 [NSURLConnection alloc] initWithRequest:request delegate:self];

(don't forget to release things if you're not using ARC)

隔纱相望 2025-01-04 16:17:21

通过称为 NSString * 也是一个 CFStringRef href="http://www.google.com/url?sa=t&rct=j&q=toll-free%20bridging&source=web&cd=4&ved=0CEgQFjAD&url=http://developer .apple.com/library/ios/d文档/CoreFoundation/概念/CFDesignConcepts/Articles/tollFreeBridgedTypes.html&ei=mgYgT5OtLcir8APo_M2fDg&usg=AFQjCNE74V6T2ZqKjRkoaQmRvdBEYQ_KGQ" rel="nofollow">免费桥接NSMutableString * 通过继承也是一个 NSString *。所以你的第二行代码应该是:

NSString *temp2 = (__bridge_transfer NSString *)
      CFURLCreateStringByAddingPercentEscapes(
         NULL,
         (CFStringRef)temp,
         NULL,
         NULL,
         kCFStringEncodingUTF8);

虽然在实践中你可能更喜欢:

NSString *temp2 = 
          [temp stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

它的第二个优点是返回带有非拥有引用的对象,所以即使你不使用它,你也不需要担心释放它弧。

An NSString * is also a CFStringRef through a mechanism known as toll-free bridging and an NSMutableString * is also an NSString * through inheritance. So your second line of code should be:

NSString *temp2 = (__bridge_transfer NSString *)
      CFURLCreateStringByAddingPercentEscapes(
         NULL,
         (CFStringRef)temp,
         NULL,
         NULL,
         kCFStringEncodingUTF8);

Though in practice you might prefer:

NSString *temp2 = 
          [temp stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Which has the secondary advantage of returning an object with a non-owning reference, so you don't need to worry about releasing it even if you're not using ARC.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文