字符串编码在目标c中

时间:2016-04-01 07:59:51

标签: ios objective-c encode

我对Objective-C很新。

我想获取NSString的编码内容。在java中我可以这样做,

String str = "https://www.google.co.in/#q=ios+sqlite+crud+example";
String encodedParam = URLEncoder.encode(str, "UTF-8");

我使用http://www.tutorialspoint.com/compile_objective-c_online.php来测试stackoverflow中发布的代码。还没有解决方案。我知道它的微不足道的一个。苦苦寻找方法。

尝试使用以下函数,并在编译时显示以下错误,

-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding {
return (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
       (CFStringRef)self,
       NULL,
       (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
       CFStringConvertNSStringEncodingToEncoding(encoding));

}

错误,

    sh-4.3$ gcc `gnustep-config --objc-flags` -L/usr/GNUstep/System/Library/Libraries -lgnustep-base -lobjc *.m -o main                                                             
    main.m: In function 'main':                                                                                                                                                     
    main.m:7:14: error: 'urlEncodeUsingEncoding' undeclared (first use in this function)                                                                                            
     -(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding {                                                                                                               
                  ^                                                                                                                                                                 
    main.m:7:14: note: each undeclared identifier is reported only once for each function it appears in                                                                             
    main.m:7:36: error: expected ';' before ':' token                                                                                                                               
     -(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding {   

根据答案进行编辑,

Patrick建议我使用以下代码,

NSString *storedURL = @"google.com/?search&q=this";
NSString *urlstring = [NSString stringWithFormat:@"http://%@/",storedURL];
NSURL *url = [NSURL URLWithString:urlstring];

NSError *error = nil;
NSStringEncoding encoding;
NSString *my_string = [[NSString alloc] initWithContentsOfURL:url
                                                 usedEncoding:&encoding 
                                                        error:&error];
NSLog (my_string);

在控制台中没有打印出来......是不是我的NSLog是对的?

由lightwolf建议,我的代码如下所示,

NSString *str = @"https://www.google.co.in/#q=ios+sqlite+crud+example";
NSString *encodedParam = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog (encodedParam);

它打印日志,但是值与str相同.....未编码...我希望这个str为

https%3A%2F%2Fwww.google.co.in%2F%23q%3Dios%2Bsqlite%2Bcrud%2Bexample

2 个答案:

答案 0 :(得分:0)

也许这就是你想要的

NSString *str = @"<html><head><title>First</title></head><body><p>Parsed HTML into a doc.</p></body></html>";
NSString *encodedParam = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

您只需编码参数,而不是编码的整个网址

答案 1 :(得分:0)

如果您想编码特定范围的字符,您可以使用

NSString *str = @"https://www.google.co.in/#q=ios+sqlite+crud+example";
NSString *encodedParam = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]];
NSLog (@"%@", encodedParam);

注意invertedSet;这样,您将编码除指定集之外的所有字符(所有字母数字)

结果是

https%3A%2F%2Fwww%2Egoogle%2Eco%2Ein%2F%23q%3Dios%2Bsqlite%2Bcrud%2Bexample

如果你想使用一组特定的字符,你应该使用

NSString *str = @"https://www.google.co.in/#q=ios+sqlite+crud+example";
NSCharacterSet* set = [NSCharacterSet characterSetWithCharactersInString:@"!*'();@&=+$,?%#[]"];
NSString *encodedParam = [str stringByAddingPercentEncodingWithAllowedCharacters:[set invertedSet]];
NSLog (@"%@", encodedParam);

在这种情况下,我故意错过了 / ,所以结果是

https://www.google.co.in/%23q%3Dios%2Bsqlite%2Bcrud%2Bexample