Search code examples
iosswiftswift3plistproperty-list

Swift, save string in property list


I've a plist file named as "data.plist" like this: enter image description here

I have six arrays, and I also have 6 buttons and a text field. I would like to save the string in the text field to the correct array when I press the right button.


Solution

  • There are many approaches, here is one:

    • Name the keys for the arrays in the plist dictionary livello_0 - livello_5.
    • Assign tags 0 - 5 to the buttons (indexes are zero-based).
    • When a button is pressed, get the tag (let tag = sender.tag) and get the array with let array = data["livello_\(tag)"].
    • Update / write the value.
    • If it is a Swift collection type, assign the array back to the dictionary (value semantics).