views:

1245

answers:

4

I have a problem with an NSData object -writeToFile: method, and the same with the NSString object as well.

When I compile to software, it gives the NSData (or NSString) may not respond to -writeToFile: message.

When I run the software it reaches this line and make an exception.

The data, I try to write into a file, is containing an ASCII file, downloaded from the internet.

Any idea is appreciated.

+3  A: 
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag;

is the method you want. So,

...
BOOL result = [data writeToFile:path atomically:atomically];
...

Should do it.

Justin
A: 

Hi,

it looks exactly like this, however I did not store the result into a BOOL.

Endre Olah
Please don't use the Answers section to comment. Use a comment for comments on your question
marcc
A: 

Hi,

I have tried it with getting the result into a BOOL, but the compilation gives the same warning message. Do I overlook something here?

Here is my code. If anybody can help.

NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [docsDirectory stringByAppendingPathComponent: Name];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = [[NSError alloc] init];

[fileManager removeItemAtPath:path error:&error];

NSURL *url = [NSURL URLWithString:@"http://www.szrt.hu/xls/luxor.csv"];

NSMutableURLRequest *liveRequest = [[NSMutableURLRequest alloc] initWithURL:url];
[liveRequest setCachePolicy:NSURLRequestReloadIgnoringCacheData];

[liveRequest setValue:@"headervalue" forHTTPHeaderField:@"headerfield"];

NSURLResponse *response;
NSData *myData = [NSURLConnection sendSynchronousRequest:liveRequest returningResponse:&response error:&error];
NSString *myString=[[NSString alloc] initWithData:myData encoding:NSASCIIStringEncoding];

[myString writeToFile:path automatically:YES encoding:NSASCIIStringEncoding];

[myData release];
[myString release];
return true;

The warnings are the followings.

/Users/endre/Documents/Luxor/Classes/LuxorFile.m:48:0 /Users/endre/Documents/Luxor/Classes/LuxorFile.m:48: warning: 'NSString' may not respond to '-writeToFile:automatically:encoding:'

/Users/endre/Documents/Luxor/Classes/LuxorFile.m:48:0 /Users/endre/Documents/Luxor/Classes/LuxorFile.m:48: warning: (Messages without a matching method signature

/Users/endre/Documents/Luxor/Classes/LuxorFile.m:48:0 /Users/endre/Documents/Luxor/Classes/LuxorFile.m:48: warning: initialization makes integer from pointer without a cast

/Users/endre/Documents/Luxor/Classes/LuxorFile.m:48:0 /Users/endre/Documents/Luxor/Classes/LuxorFile.m:48: warning: unused variable 'writeResult'

/Users/endre/Documents/Luxor/Classes/LuxorFile.m:68:0 /Users/endre/Documents/Luxor/Classes/LuxorFile.m:68: warning: 'NSString' may not respond to '-writeToFile:atomically:encoding:'

Thanks in advance for any help.

Endre Olah
A: 

Hi,

actually I have found my mistake. NSString is deprecated so NSData must be used, but I must have a typing error somewhere.

Endre Olah