So I have a user drop some files into my application. Later I'm handling these files using NSFileHandle like this:
NSFileHandle *inputFile = [NSFileHandle fileHandleForReadingFromURL:path error:&error];
if(inputFile == nil) {
NSLog(#"Error (2): %#",error);
return;
}
On my Mac it works, but on other devices this error occurs:
Error Domain=NSCocoaErrorDomain Code=3 "The operation couldn't be completed. (Cocoa error 3.)" ...
I have looked into FoundationErrors.h but there is no error code 3 and i couldn't find anything useful using google.
What is causing this error (the description doesn't give much detail) and how can i prevent it?
Related
I'm creating a watch application which records an audio and play that audio. The recorded audio is saved in the Apps Groups and I used the following code for playing the audio.
- (void)playAudio
{
// Playing the audio from the url using the default controller
[self presentMediaPlayerControllerWithURL:self.audioFileURL options:#{WKMediaPlayerControllerOptionsAutoplayKey : #YES} completion:^(BOOL didPlayToEnd, NSTimeInterval endTime, NSError * _Nullable error) {
NSLog(#"Error = %#",error);
}];
}
Unfortunately I am getting the following error and the player controller get dismissed immediately.
Error Domain=com.apple.watchkit.errors Code=4 "The operation could not be completed" UserInfo={NSLocalizedFailureReason=An unknown error occurred (1), NSUnderlyingError=0x16daa810 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}, NSLocalizedDescription=The operation could not be completed}.
Then I downloaded a sample project (WatchKitAudioRecorder Sample code) from Apple WatchOS Developer library but they are also having the same issue. I don't know why this is not working even in Apple Sample code :(
Finally I found that the data is not writing to the url path. Also should be written in the following path: Library/Caches/filename.extension
Here is a sample code, which may help someone.
- (void)writeAudioToAppGroupsWithData:(NSData *)audioData
{
// Writing the audio data to the App Groups
NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:APP_GROUP_IDENTIFIER];
containerURL = [containerURL URLByAppendingPathComponent:[NSString stringWithFormat:DIRECTORY_PATH]];
[audioData writeToURL:containerURL atomically:YES];
}
In this case make sure that your DIRECTORY_PATH is Library/Caches/filename.extension. Eg: Library/Caches/Audio.mp3
I get a weird issue while saving a file with writeToFile:options:error: In the first case I get the following issue:
writeToFile failed with error Error Domain=NSCocoaErrorDomain Code=4 "The file “preferences.plist” doesn’t exist." UserInfo=0xa12c30 {NSFilePath=file:/Users/patrick/Desktop/Untitled.fef/preferences.plist, NSUnderlyingError=0xa0d130 "The operation couldn’t be completed. No such file or directory"}
But the file actually exist in that location. The file is stored in the app document file package.
CODE: (Subclass of NSDocument)
NSString *prefFile = [[[self fileURL] absoluteString] stringByAppendingPathComponent:#"preferences.plist"];
NSError *error;
BOOL succes = [[NSKeyedArchiver archivedDataWithRootObject:documentPreferences] writeToFile:prefFile options:0 error:&error];
if (!succes) {
NSLog(#"writeToFile failed with error %#", error);
}
Make sure when using any of the ...toFilemethods of various classes that you use a real path and not by mistake a file URL. You can tell these apart by looking at the path. If the path starts with file://it can't be used. If it starts with a slash / it is a regular file path. Still you might use NSFileManager to check if the file exists.
As in your code instead of calling absoluteString on self.fileURL call path.
I am very new to the iPhone development and although I spent hours in searching for the right answer I only got more confused at the end.
Here is my question - I'm making a simple iPhone application which has its own database/consisting of only one simple table and being stored in my projects folder along with all the .m and .h files/ and which uses the managed object model presented by core data.
I think I did everything the way it should be done but my program ends with the following "Unresolved error Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)
Actually here is the whole output:
2011-12-07 18:50:50.009 weatherForecast[5368:207] CoreData: error: (1) I/O error for
database at /Users/eln/Library/Application Support/iPhone
Simulator/5.0/Applications/4991C3EB-BDC2-4507-B1FB-720F6DC30245/Documents/weatherForecast.sqlite.
SQLite error code:1, 'no such table: Z_METADATA'
2011-12-07 18:50:50.012 weatherForecast[5368:207] Unresolved error
Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be
completed. (Cocoa error 256.)" UserInfo=0x6a33e40
{NSUnderlyingException=I/O error for database at
/Users/toma.popov/Library/Application Support/iPhone
Simulator/5.0/Applications/4991C3EB-BDC2-4507-B1FB-720F6DC30245/Documents/weatherForecast.sqlite.
SQLite error code:1, 'no such table: Z_METADATA',
NSSQLiteErrorDomain=1},
{
NSSQLiteErrorDomain = 1;
NSUnderlyingException = "I/O error for database at /Users/eln/Library/Application Support/iPhone
Simulator/5.0/Applications/4991C3EB-BDC2-4507-B1FB-720F6DC30245/Documents/weatherForecast.sqlite.
**SQLite error code:1, 'no such table: Z_METADATA'";
my program stops right in this get method:
**
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil)
{
return __persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:#"weatherForecast.sqlite"];
NSLog(#"DATABASE IS LOCATED AT: %#",(NSString *)[self applicationDocumentsDirectory]);
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
{
//right here my program aborts
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
**
I will be much grateful if someone gives me advise or at least suggestion on what might the problem be.Thank you in advance!
Core Data needs to manage/create its own database, it can't use one that you've created yourself. Is weatherForecast.sqlite such a file?
I wrote some code to download a URL to my disk, using an NSURLRequest and an NSURLDownload. The download starts fine (at least, it sends downloadDidBegin:(NSURLDownload *)download), but then it sends
-(void)download:(NSURLDownload *)download didFailWithError:(NSError *)error
My implementation of the function is as follows:
{
[download release];
NSLog(#"Download failed! Error - %# %#",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
The logged error is
2011-07-17 01:48:43.194 URLDownloader[28031:903] Download failed! Error - The operation couldn’t be completed. (NSURLErrorDomain error -3001.) [the URL in the NSURLRequest]
From what I can tell, this is the NSURLErrorCannotOpenFile error. I cannot find any explanation of why this error occurs, other then "Returned when NSURLDownload was unable to open the downloaded file on disk." from the documentation. So, why is this happening?
Fact that might be helpful(?): NSURLErrorCannotOpenFile is enumerated as kCFURLErrorCannotOpenFile. I have no idea what this means either....
Alright, so I was just being foolish. I had created a path using NSURL's absoluteString method instead of the path method. This was just bad coding on my part...
I've got a small utility application that parses a csv-file and uses the data to fill a Core Data store for use with another application. I open the csv-file using -initWithContentsOfFile:encoding:error: and the method always returns nil with the error
Error Domain=NSCocoaErrorDomain Code=260 UserInfo=0x100106450 "The file “data.csv” couldn’t be
opened because there is no such file." Underlying Error=(Error Domain=NSPOSIXErrorDomain Code=2 "The
operation couldn’t be completed. No such file or directory"), {
NSFilePath = "/Users/****/Library/Developer/Xcode/DerivedData/CreateData-
bhkmspyczgcfcrgehcbaydwdbfoa/Build/Products/Debug/data.csv";
NSUnderlyingError = "Error Domain=NSPOSIXErrorDomain Code=2 \"The operation couldn\U2019t be
completed. No such file or directory\"";
}
Now, I'm looking at that exact file in that exact directory. Even more confusing is the fact that I have another version of essentially the same utility for another app that works -- the only difference is the makeup of the core data store and entities and the number of columns in the csv file, all of which are invoked after loading the csv file. Or failing to load, as it were ...
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSError *error = nil;
NSString *csvFile = [[NSString alloc] initWithContentsOfFile:csvFilePath(QUESTIONS_INPUT_FILENAME) encoding:NSASCIIStringEncoding error:&error]; // This fails
if (error != nil) {
NSLog(#"Returned error:\n%#, %#", error, [error userInfo]); // Returns the above error message
exit(1);
}
// ...
}
NSString *csvFilePath(NSString *inputFileName) {
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
return [resourcePath stringByAppendingPathComponent:inputFileName];
}
NSString *applicationDocumentsDirectory() {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
It fails because you aren't passing in the actual location of the file, just the filename, so it is trying to look for it in the program's own folder - you're using the path of the bundle. unless that's where your csv file is, it won't find it.
To put it simply the file path you passed is incorrect thus can't be found or does not exist.