I would like to read in an Array I have stored in a PLIST, modify an element, and store it back to the PLIST.
My simple code is as follows:
NSArray *mathScoreArray;
NSNumber *score1 = [NSNumber numberWithInteger:score];
NSMutableArray *scoreArray1 = [[NSArray arrayWithArray:mathScoreArray] init];
[scoreArray1 replaceObjectAtIndex:4 withObject:score1];
[dictionary setObject:scoreArray1 forKey :@"mathScoreArray"];
[dictionary writeToFile:finalPath atomically:YES];
I’m getting errors for the ‘replaceObjectAtIndex’ for modifying the array.
How can I do this??
Just for reference, my plist looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>mathQues</key>
<integer>1</integer>
<key>mathScore</key>
<integer>0</integer>
<key>mathScoreArray</key>
<array>
<integer>10</integer>
<integer>20</integer>
<integer>30</integer>
<integer>40</integer>
<integer>50</integer>
</array>
</dict>
</plist>
Extending the problem, eventually I would like to shift the array one place to right and store a new incoming value on top [just like a stack].
Thanks for pointers.
user
Change this line:
to:
Also, in your code there is no sign of how did you read the
NSArrayto begin with.