views:

64

answers:

4

I would like to edit a plist from Xcode which I will use to fill the table view.

The thing is I have an array of images, and the user can add one of these images to a favorites list.

This needs to be written to the plist. So I need the index number of the image and the name they choose to save it with. How can I write to a plist?

Then when they view favorites and they select an item in the table, that needs to load and display the appropriate image.

I was thinking of writing the name they choose with the number of the index appended with for e.g. "index_titleSaved" and then when reading from the plist I could just grab the number before the first _. and then show it.

Is this a good way or could it be done more efficiently?

Thanks in advance.

+1  A: 

You can use an NSDictionary's writeToFile method, or, if you want an array (or an array of dictionarys), use NSArray's writeToFile method.

jrtc27
re-posting as an answer for code clarity. :)
alJaree
A: 

I am still not sure how to do this. I have tried this

NSString *filepath = [[NSBundle mainBundle] 
                    pathForResource:@"tableViewData" ofType:@"plist"];
NSArray* plistArray = [[NSArray alloc] initWithContentsOfFile:filepath];

[plistArray setValue:(@"%@",title) forKey:@"Item 0"];
[plistArray writeToFile:filepath atomically: YES];

The app crashes after trying to execute this.

error message

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Item 0.'

alJaree
I'm not sure how you have your plist set up, but think of the key like this... Key: Animal , Value: Dog ... Key: Animal Name , Value: Pluto ..Item 0 is just your first item in an array most likely. The key is nested one level under that (again, I'm only guessing without knowing how you set it up)
iWasRobbed
A: 

According to your second question, I think your app crash because Array use a numeric key and can't use NSString as key. If you need a key->value structure you have to use NSDictionary

Noya