1

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?

1 Answer 1

4

Are you sure self.myArray is 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 with self.myArray returning nil, 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.myArray is 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.

Sign up to request clarification or add additional context in comments.

1 Comment

You were right, I messed the code reimplementing and didn't initialize the property, but since I have never save an array to disk before I just thought I did something wrong with that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.