I don't know how to use this method.
I want to get the data in an rtf file as a NSString, however I can not.
Here is part of my code.
NSString *filePath = #"path of the file";
NSString *ImagePath = [[[NSString alloc] init] autorelease];
imagePath = [NSString stringWithContentsOfFile: filePath encoding:(NSStringEncoding)nil error:(NSError**) nil];
I can't readout the data in that .rtf file. I am appreciate that everyone may help me. Thanks.
Try using this
imagePath = [NSString stringWithContentsOfFile: filePath encoding:NSUTF8StringEncoding error:nil];
Here is the function
NSString *content = [[[NSString alloc] initWithContentsOfFile:filepath
usedEncoding:nil
error:nil] autorelease];
before using this be sure u giving a right path (filepath) to the string.
Related
I try to read string from a file with :
NSString* path = [[NSBundle mainBundle] pathForResource:#"dirty" ofType:#"txt"];
NSString* content = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];
And the string i get is nil. in the error i get :
Error Domain=NSCocoaErrorDomain Code=261 "The operation couldn’t be completed. (Cocoa error 261.)" UserInfo=0x21063410 {NSFilePath=/var/mobile/Applications/78889F89-B83C-480C-A246-999152EE757C/Myapp.app/dirty.txt, NSStringEncoding=4}
Any idea what is the problem?
As mentioned, Cocoa error 261 is NSFileReadInapplicableStringEncodingError which means the encoding of file is different than what you are passing in the API.
For Hebrew language encoding, try this answer
NSString* content = [[NSString alloc] initWithContentsOfFile:path encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingWindowsHebrew) error:&err];
Hope that helps!
The error seems to be the Encoding Issue ..
If you don't know the encoding of your file, you could try this method:
- (id)initWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error
Refer HERE
And
Try Something like this:
NSError *error = nil;
NSStringEncoding encoding;
NSString *my_string = [[NSString alloc] initWithContentsOfURL:url
usedEncoding:&encoding
error:&error];
Try this.
NSString* content = [[NSString alloc] initWithContentsOfFile:path encoding:nil error:&err];
or change the type of "encoding" hope it will solve you problem.
Try This
NSString *pathtest = [[NSBundle mainBundle] pathForResource:#"dirty" ofType:#"txt"];
NSString* content = [[NSString alloc] initWithContentsOfFile:pathtest encoding:NSASCIIStringEncoding error:&error];
NSLog(#"Test: %#",content);
Here are the code I tried to convert NSData to NSString but the program return "Program received signal:SIGABRT".
NSString *string= [NSString stringWithUTF8String:[data bytes]];
OR
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
Is there any other better way to do it?
Here's a highly up-voted answer that shows how to do it. In a nutshell:
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
The first idea looks like a fail because of sending [data bytes]. stringWithUTF8String isn't prepared for a void *. The second idea looks as if it should work, even with nil input.
Use this code
NSString* myString;
myString = [[NSString alloc] initWithData:nsdata encoding:NSASCIIStringEncoding];
also see this tutorial..
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html
:)
Swift-3
let string = data.base64EncodedString()
i getting data from my site:
NSString *website = [NSString stringWithFormat:#"http://www.mysite.com/fbconnect.php?email=%#&name=%#&pass=***", nameTrimmmed, [jsonObject objectForKey:#"email"]];
NSLog(#"%#", website);
NSError *error = nil;
NSString *contents = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:website] encoding:NSUTF8StringEncoding error:&error];
contents have Cocoa error 256. where i wrong?
The issue is in Hebrew characters, you should html-escape them, also try request with English characters instead, to see if it works
- (void)yourMethod
{
NSString *name = #"שימרגוליס";
name = AFURLEncodedStringFromStringWithEncoding(name, NSUTF8StringEncoding);
NSString *website = [NSString stringWithFormat:#"http://www.ba-cafe.com/fbconnect.php?email=%#&name=%#&pass=SwHyK17!",#"email#mail.com",name];
NSLog(#"%#", website);
NSError *error = nil;
NSString *contents = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:website] encoding:NSUTF8StringEncoding error:&error];
}
Where AFURLEncodedStringFromStringWithEncodingis a function from AFNetworking framework
Check the Console log for NSLog(#"%#", website);
You will see something like this:
http://www.mysite.com/fbconnect.php?email=thetrimmedname&name=emailaddress&pass=***
So do this:
NSString *website = [NSString stringWithFormat:#"http://www.mysite.com/fbconnect.php?email=%#&name=%#&pass=***", [jsonObject objectForKey:#"email"], nameTrimmmed ];
instead of this:
NSString *website = [NSString stringWithFormat:#"http://www.mysite.com/fbconnect.php?email=%#&name=%#&pass=***", nameTrimmmed, [jsonObject objectForKey:#"email"]];
This is because of the dot in the email address.
Look right here:
Error while trying to access Google translate with NSURL
I want to convert one NSString to NSData. on Using encoding am getting different value in NSData by displaying it using its description property.
I have
NSString *str=#"80369F4";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
on printing [data description]; am getting some different values...
how to extract the same string from the NSData.. pl help me out on this problem.
Create a new NSString:
NSString *newString = [[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding] autorelease];
You can use NSString's initWithData:encoding: method:
NSString *str=#"80369F4";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSString *sameString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
self.pdfPath = [[NSBundle mainBundle] pathForResource:#"iPhone_SDK_License" ofType:#"pdf"];
NSData *htmlData = [NSData dataWithContentsOfFile:pdfPath];
NSString *htmlstr = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
It is showing nil in htmlstr, so need some assistance on it.
A pdf is not an html file so I assume that initWithData:encoding: just cannot understand the data and so fails.
Use stringWithContentsOfURL:encoding:error: to see the details of the error.