I don't know why it is working on XCode 3, but the problem is that -(instancetype)arrayWithContentsOfFile:filePath
returns nil
if the file does not exist, which is likely the case the first time the code is called. Thus, when you are calling methods on the array, nothing happens and 0
or nil
is returned, which is why you are getting zero for the length of the nil array and the item is not being added.
You need to add a condition to check if the file exists before retrieving it.
I have tested your code in my project and changing the 5th line to the following worked:
NSMutableArray *array;
if ([[NSFileManager defaultManager] fileExistsAtPath:choice]) {
array = [NSMutableArray arrayWithContentsOfFile:choice];
}
else {
array = [[NSMutableArray alloc] init];
}
This way, instead of getting a nil object when the file does not exist, you create a new array instead and use that to store the high scores.