嗨,我必须通过 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
发布评论
评论(2)
你为什么使用 CFURL & CFStringRef 函数在这里?
您可以通过 NSString 的
stringByAddingPercentEscapesUsingEncoding:
方法。我已经为您链接了文档。像这样的东西:(
如果你不使用 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:
(don't forget to release things if you're not using ARC)
通过称为 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 *
。所以你的第二行代码应该是:虽然在实践中你可能更喜欢:
它的第二个优点是返回带有非拥有引用的对象,所以即使你不使用它,你也不需要担心释放它弧。
An
NSString *
is also aCFStringRef
through a mechanism known as toll-free bridging and anNSMutableString *
is also anNSString *
through inheritance. So your second line of code should be:Though in practice you might prefer:
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.