Endre Oláh
Endre Oláh

Reputation: 11

NSData writeToFile gives a "may not respond to" message on iPhone SDK

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.

Update:

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:'

Upvotes: 1

Views: 3345

Answers (1)

justin
justin

Reputation: 104698

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

is the method you want. So,

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

Should do it.

Upvotes: 3

Related Questions