using NSString + stringWithContentsOfFile:usedEncoding:error: - iphone

I've got problem with use + stringWithContentsOfFile:usedEncoding:error:
My problem in usedEncoding:(NSStringEncoding *)enc
I don't know how can i set pointer to encoding. If i make it - programm is fail.
For example, in similar function we have encoding:(NSStringEncoding)enc - without pointer!
I want loading file (file has encoding ISOLatin1) in NSString and use NSString as UTF8String.
how can i make it ?
thanks.

NSStringEncoding encoding;
NSError* error;
NSString* myString = [NSString stringWithContentsOfFile:myFilePath usedEncoding:&encoding error:&error];

Related

NSUTF8StringEncoding or stringWithUTF8String

I am having some issues in dealing with characters in NSStrings.
I read a XML file converted to NSData, it is okay, but when I transfer the "Element name" it has not converted to UTF-8.
I've tried here are some examples of the site, but with no success.
My Code is -
NSString * S = [NSString stringWithFormat: # "%#", D];
S = [S stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
Try this..
NSString *url = S;
NSString *check=[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
check=[check stringByReplacingOccurrencesOfString:#"%0A" withString:#""];

Special characters xml parsing issue in iphone

When i try to parse xml containing email address say john#abc.com, it just shows "abc.com".
How can i make it to show the complete email address. In other cases i've removed some special charcters by using the following:-
string=[string stringByReplacingOccurrencesOfString:#"$" withString:#""];
but here i've to include the symbol "#" and characters before it.
Thanks for any help.
Well... finally found a solution for myself. I converted the xml data into a string and replaced characters. Below is the code:-
NSError* error;
NSString *content = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];
content=[content stringByReplacingOccurrencesOfString:#"#" withString:#"#"];
NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
Problem Solved :)
NSString *string = #"$amdocs.com";
string=[string stringByReplacingOccurrencesOfString:#"$" withString:#"#"];
NSLog(#"%#",string);
maybe it will help you.

iOS - how to read entire content of an html resource file into an NSString *?

I want to put the content of my html resource file into an NSString object. Is it possible and advisable to do that? How could it be done?
Possible? - yes
Advisable? - unless it is an extremely large file, why not?
How? - There is already a method to do it for you in NSString - stringWithContentsOfFile:encoding:error:.
See the snippet below:
NSError* error = nil;
NSString *path = [[NSBundle mainBundle] pathForResource: #"foo" ofType: #"html"];
NSString *res = [NSString stringWithContentsOfFile: path encoding:NSUTF8StringEncoding error: &error];

Function deprecated in ios 4

Hi all i update to ios 4 but in my app i use :
NSString *connected = [NSString string withContentofURL:[NSURL URLWithString:#"http://myurl.com/myFile]];
but now i get the following :
StringWithContentsofURL is deprecated !
I use this to test if connection is available.
What can i do ??
thanks
As of iPhone OS2 (so this is not new) the
[NSString withContentsOfURL: (NSURL*) url]
method has been replaced with
+ (id)stringWithContentsOfURL: (NSURL *)url encoding: (NSStringEncoding)enc error: (NSError **)error
Here's an example using the new signature:
NSError* error = nil;
NSURL* url = [NSURL urlWithString: #"www.google.com"];
NSString* stringForUrlPath = [NSString stringWithContentsOfURL: url
encoding: NSUTF8StringEncoding
error: &error];
See this for your options for NSStringEncoding.
A little Googling will give you the answer here:
It has been replaced with stringWithContentsOfURL:encoding:error...
Source: What is the "stringWithContentsOfURL" replacement for objective C?
Also, in the future, please try to spell check your inquiries.
Use
NSError* error;
NSString *string = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
instead. Apple shouldn't have included stringWithContentsOfURL: (without encoding) to start with. It has been deprecated on OS X for a long time, because that was the cause of a lot of headaches for non-English speakers.
That said, don't download something to test the connectivity. Instead, use Reachiability.

How do I parse a text file in Objective-C?

I need to parse a text file, one line at a time. Also, is there EOF in Objective-C?
Something like this might work for you:
NSString *fileContents = [NSString stringWithContentsOfFile:#"myfile.txt"];
NSArray *lines = [fileContents componentsSeparatedByString:#"\n"];
This will give you an array where each element is a line of the string.
Objective-C is a proper extension of C. Any C program is a valid Objective-C program. Among other things, this means that EOF defined in the standard C header "stdio.h" is an EOF marker in Objective-C as well.
stringWithContentsOfFile is deprecated.
Here is an updated answer:
NSError* error;
NSString *fileContent = [NSString stringWithContentsOfFile:txtFilePath encoding:NSUTF8StringEncoding error:&error];
NSArray *lines = [fileContent componentsSeparatedByString:#"\n"];