I am working on an app that parses XML responses from a web service. This web service will return several instances of an object of many different types using object stacking. I just want to take these objects and populate them into an array. Right now I have a switch that is repeating several different lines of similar code based on "objectStack.last" type. This is what it looks like right now in the parserDidEndElement function of the XML parser:
switch(objectStack.last){
case _ as apple:
apples.removeAll()
for apple in objectStack as! [apple]{
apples.append(apple.name.value)
objectType = "apple"
}
case _ as orage:
oranges.removeAll()
for orange in objectStack as! [orange]{
oranges.append(orange.name.value)
}
objectType = "orange"
case _ as bananas:
bananas.removeAll()
for banana in objectStack as! [banana]{
bananas.append(banana.name.value)
}
objectType = "banana"
case default:
break
The problem is that the webservice will be returning a large number of different types, and I would rather not have to build a switch case for each. For the purposes of this discussion, let's say I have created an empty array for each type just by adding a "s" to the type name (ie apples, oranges and bananas). Is there a way for me to manipulate the arrays using a variable? Something like this:
objectStack.last + "s".removeAll()
for objectStack.last in objectStack.last + "s" as! [objectStack.last]{
objectStack.last + "s".append(objectStack.last.name.value)
objectType = objectStack.last
}
Obviously this second code block is not valid code, I'm just hoping it helps to illustrate what I am trying to accomplish.
Thanks in advance for your help!