如何使用stringWithContentsOfURL:encoding:error:?

时间:2022-05-03 00:19:16

I am trying to use initWithContentsOfURL:encoding:error: like this :

我试图使用initWithContentsOfURL:encoding:error:像这样:


NSURL *url = [[NSURL alloc] initWithString:@"http://my_url.com/my_file.xml"];
NSError *error = nil;
NSString *my_string = [[NSString alloc] initWithContentsOfURL:url
encoding:NSUTF8StringEncoding
error:&error];

I get a empty my_string variable.

我得到一个空的my_string变量。

I tried the initWithContentsOfURL: method (which is deprecated in iOS 2.0) and I get the content of my page. But I still need to specify a encoding language.

我尝试了initWithContentsOfURL:方法(在iOS 2.0中已弃用),我得到了我的页面内容。但我仍然需要指定一种编码语言。

What's wrong ?

怎么了 ?

Thanks :)

谢谢 :)

3 个解决方案

#1


33  

the encoding of your file is probably not UTF8.

您的文件的编码可能不是UTF8。

If you don't know the encoding of your file, you could try this method:

如果您不知道文件的编码,可以尝试以下方法:

- (id)initWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError **)error

you have to pass a pointer to a NSStringEncoding, like you did with error.:

你必须传递指向NSStringEncoding的指针,就像你做错了一样:

NSURL *url = [[NSURL alloc] initWithString:@"http://my_url.com/my_file.xml"];
NSError *error = nil;
NSStringEncoding encoding;
//NSString *my_string = [[NSString alloc] initWithContentsOfURL:url
//                                                     encoding:NSUTF8StringEncoding
//                                                        error:&error];
NSString *my_string = [[NSString alloc] initWithContentsOfURL:url
                                                 usedEncoding:&encoding 
                                                        error:&error];

after this your encoding is present in the encoding variable. If you are not interested in the used encoding, I guess you could pass NULL as pointer as well.

在此之后,您的编码将出现在编码变量中。如果您对使用的编码不感兴趣,我猜您也可以将NULL作为指针传递。

#2


3  

Why not use a request and connection to get the info back in an NSData object? Something like this:

为什么不使用请求和连接来获取NSData对象中的信息?像这样的东西:

NSURL *url = [[NSURL alloc] initWithString:@"http://my_url.com/my_file.xml"];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPMethod:@"GET"];

NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];

    [conn start];

if(conn){
    // Data Received
    responseData = [[NSMutableData alloc] init];
}

and then in your connection:didRecieveData delegate method, put something like this

然后在你的连接:didRecieveData委托方法,把这样的东西

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

[self.responseData appendData:data];
}

and then once the connection is finished loading convert the data to a string:

然后一旦连接完成加载,将数据转换为字符串:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
     NSString *string = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];
}

Not the most straightforward method, but that should get you your XML string. Also if you need to parse the XML once you get it back, you can directly pass the responseData to an NSXMLParser without any conversion. :)

这不是最简单的方法,但应该为您提供XML字符串。此外,如果您需要在获取XML后解析XML,则可以直接将responseData传递给NSXMLParser而无需任何转换。 :)

#3


-1  

You can modify your webpage by adding header('Content-Type: text/html; charset=UTF-8'); to the top of the code and saving the document as a UTF-8 formated file. It helped me as I had the same problem.

您可以通过添加标题来修改您的网页('Content-Type:text / html; charset = UTF-8');到代码的顶部并将文档保存为UTF-8格式化文件。它帮助了我,因为我遇到了同样的问题。

#1


33  

the encoding of your file is probably not UTF8.

您的文件的编码可能不是UTF8。

If you don't know the encoding of your file, you could try this method:

如果您不知道文件的编码,可以尝试以下方法:

- (id)initWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError **)error

you have to pass a pointer to a NSStringEncoding, like you did with error.:

你必须传递指向NSStringEncoding的指针,就像你做错了一样:

NSURL *url = [[NSURL alloc] initWithString:@"http://my_url.com/my_file.xml"];
NSError *error = nil;
NSStringEncoding encoding;
//NSString *my_string = [[NSString alloc] initWithContentsOfURL:url
//                                                     encoding:NSUTF8StringEncoding
//                                                        error:&error];
NSString *my_string = [[NSString alloc] initWithContentsOfURL:url
                                                 usedEncoding:&encoding 
                                                        error:&error];

after this your encoding is present in the encoding variable. If you are not interested in the used encoding, I guess you could pass NULL as pointer as well.

在此之后,您的编码将出现在编码变量中。如果您对使用的编码不感兴趣,我猜您也可以将NULL作为指针传递。

#2


3  

Why not use a request and connection to get the info back in an NSData object? Something like this:

为什么不使用请求和连接来获取NSData对象中的信息?像这样的东西:

NSURL *url = [[NSURL alloc] initWithString:@"http://my_url.com/my_file.xml"];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPMethod:@"GET"];

NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];

    [conn start];

if(conn){
    // Data Received
    responseData = [[NSMutableData alloc] init];
}

and then in your connection:didRecieveData delegate method, put something like this

然后在你的连接:didRecieveData委托方法,把这样的东西

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

[self.responseData appendData:data];
}

and then once the connection is finished loading convert the data to a string:

然后一旦连接完成加载,将数据转换为字符串:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
     NSString *string = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];
}

Not the most straightforward method, but that should get you your XML string. Also if you need to parse the XML once you get it back, you can directly pass the responseData to an NSXMLParser without any conversion. :)

这不是最简单的方法,但应该为您提供XML字符串。此外,如果您需要在获取XML后解析XML,则可以直接将responseData传递给NSXMLParser而无需任何转换。 :)

#3


-1  

You can modify your webpage by adding header('Content-Type: text/html; charset=UTF-8'); to the top of the code and saving the document as a UTF-8 formated file. It helped me as I had the same problem.

您可以通过添加标题来修改您的网页('Content-Type:text / html; charset = UTF-8');到代码的顶部并将文档保存为UTF-8格式化文件。它帮助了我,因为我遇到了同样的问题。