fails, but no error object is returned. Why?
-
13-09-2019 - |
Question
I'm trying to understand the meaning of the value returned by [NSData writeToFile:options:error:]. The method returns a BOOL, which according to Apple's documentation is "YES if the operation succeeds, otherwise NO."
Fair enough, but if it's NO, I would have assumed that the error parameter would then be set to some retrievable NSError* value. However in results I'm coming across, that's not the case. Accordingly I'm somewhat confused, and don't know how to determine what caused the failure.
To wit, I've got this code (more or less):
NSError* error = nil;
BOOL success = [data writeToFile: filePath error: &error];
if ( error )
NSLog( @"error = %@", [error description] );
success
turns out to be NO
in the code I'm running, but the NSLog
statement is never executed. How come?
Howard
Solution
It's possible that data
is nil
, in which case [data writeToFile:error:]
returns nil
, but *error
is not set.
OTHER TIPS
The writeToFile method returns TRUE on success and FALSE on failure -that's what you want to actually check for.
So, Try:
if(!success)
As your conditional instead of if( error ).