1

I'm trying to modify an existing struct which is then used with an array. Is there a solution to the following?

struct pickerData {
    var key = ""
    var value = ""
}

var pickerArray = [pickerData]()
pickerArray.append(pickerData(key: "1", value: "2")) //OK up to know but
//I need to append a new key:value to this structure

pickerArray.append(pickerData(key: "1", value: "2",value2: "3")) // error
pickerArray.append(pickerData(key: "1", value: "2"),value2: "3") // error

I basically need a mutable struct, is this doable?

2
  • 1
    It's not clear what you're after, you want to extend pickerData with more properties? This isn't possible – it could be that you want pickerData.value to be an Array or Dictionary Commented Apr 13, 2015 at 19:33
  • I'm trying to build an array with some key:value pairs where I don't yet know how many key:pair I will have nor will I know the key name. As an example I could start with an empty array and dynamically add new key:pairs in a for loop. The Arry would start with () then (key: "1"), then (key: "1", value: "2") then (key: "1", value: "2",value2: "3") and so on. Commented Apr 13, 2015 at 22:25

1 Answer 1

1

The structure of a struct cannot change; as your comment suggests that you want to add a key:value pair, you should be using a datatype that supports such pairs: a Dictionary. (Technically, those aren't key:value pairs in a struct.)

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

Comments

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.