I have a NSMutableArray. It can be empty or have a number of propertyList objects (In my case just NSNumber objects).
I want to save it to disk in xml format, so I’m using.
// In the interface
@property (nonatomic, retain) NSMutableArray *myArray;
// In the implementation
if ([self.myArray writeToFile:pathToFile atomically:YES])
{
// Success
} else {
// Failure
}
If the array is empty ( an empty initialized NSMutableArray) in the moment the method is called the method returns NO and the operation is not done, so the old data is not overwritten.
How can I write an empty array to disk?
Are you sure
self.myArrayis in fact an “empty initialized NSMutableArray”? For me, telling an empty array to write itself to a file works fine.The behavior you’re observing, that the message returns
NO, is consistent withself.myArrayreturningnil, which means it is neither empty nor non-empty, because you don’t have an array at all. With no array, there is nothing to send a message to, so the message returns (in this case)NO.Make absolutely sure that
self.myArrayis in fact returning an array, empty or otherwise, at the point where you’re trying to send it a message. The logging code and output you used to verify that would be a great thing for you to add to your question.