用Objective-C编写一个字符串 [英] URLEncoding a string with Objective-C
问题描述
我试图对一个字符串进行URL编码以形成来自objective-c的GET请求。
NSString * params = @'解码数据!'/ foo.bar:baz;
NSRunAlertPanel(@Error,[params urlEncoded],@OK,nil,nil);
这是扩展NSString的类别
- (NSString *)urlEncoded
{
NSString * encoded =(NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)self,
NULL,
(CFStringRef)@!*'\();:@& = + $,/?%#[]%,
kCFStringEncodingUTF8);
return b;
}
所以我第一次运行它回到
1606410046ecoded 1606410784ata2270.000000foo.bar0X1.001716P-1042baz
在我再次运行之后,我立即得到
1606410046ecoded 1606410944ata227369374562920703448982951250259562309742470533728899744288431318481119278377104028261651081181287077973859930826299575521579020410425419424562236383226511593137467590082636817579938932512039895040.000000foo.bar0X1.66E6156303225P + 771baz
然后,如果我运行它,它回到第一个。非常奇怪。
如果params设置为@&或@我只是回来一个2(w / o的报价)在对话框中。
也有一种方法,我可以有%
我认为 NSAlert
正在将%
字符解释为以随机数据填充的字符串格式说明符。 NSLog
输出并且很好:
%27Decoded%20data% 21%27%2Ffoo.bar%3Abaz
此外,您的 -urlEncoded
类别方法。您可以使用包含创建
的CF函数创建字符串,以便您负责发布该字符串。
- (NSString *)urlEncoded
{
CFStringRef urlString = CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)self,
NULL,
(CFStringRef)@!*'\();:@& = + $,/?%#[]%,
kCFStringEncodingUTF8);
return [(NSString *)urlString autorelease ];
}
I'm trying to URL encode a string to form a GET request from objective-c.
NSString *params = @"'Decoded data!'/foo.bar:baz";
NSRunAlertPanel( @"Error", [params urlEncoded], @"OK", nil, nil );
This is the category extending NSString
-(NSString *) urlEncoded
{
NSString *encoded = (NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)self,
NULL,
(CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
kCFStringEncodingUTF8 );
return encoded;
}
So the first time I run it I get back
1606410046ecoded 1606410784ata2270.000000foo.bar0X1.001716P-1042baz
from the dialog box.
Immediately after I run it again I get this
1606410046ecoded 1606410944ata227369374562920703448982951250259562309742470533728899744288431318481119278377104028261651081181287077973859930826299575521579020410425419424562236383226511593137467590082636817579938932512039895040.000000foo.bar0X1.66E6156303225P+771baz
Then if I run it AGAIN it goes back to the first one. It's really weird.
If params is set to @"&" or @" " I just get back a "2" (w/o the quotes) in the dialog box.
Also is there a way I can have the % signs be shown in the alert dialog?
Thanks
I think the NSAlert
is interpreting the %
characters as string format specifiers which are being filled with random data. Just NSLog
the output and it's fine:
%27Decoded%20data%21%27%2Ffoo.bar%3Abaz
Also, you have a memory leak in your -urlEncoded
category method. You create the string using a CF function containing Create
so you are responsible for releasing it.
-(NSString *) urlEncoded
{
CFStringRef urlString = CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)self,
NULL,
(CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
kCFStringEncodingUTF8 );
return [(NSString *)urlString autorelease];
}
这篇关于用Objective-C编写一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!