As @CodeDifferent writes in his/her answer, you might want to consider remodelling your data as this type of runtime property accessing is not very "Swifty".
If you're only interesting in reading data for the sake of debugging, however, you could make use of the Mirror
structure to perform runtime introspection on the data type that owns the array properties.
E.g.:
struct Foo {
let fruitsArray = ["apple", "orange", "banana"]
let appleArray = ["red", "green", "yellow"]
}
func attemptToReadStringArrayProperty(_ name: String, fromFoo foo: Foo) -> [String]? {
return Mirror(reflecting: foo).children
.flatMap { ($0 ?? name + ".") == name ? ($1 as? [String]) : nil }.first
}
/* example usage/debugging */
let string1 = "apple"
let string2 = "Array"
let string3 = string1 + string2
let foo = Foo()
if let strArr = attemptToReadStringArrayProperty(string3, fromFoo: foo) {
strArr.forEach { print($0) }
} /* red
green
yellow */
Naturally, you could apply this approach for non-debugging purposes, but I wouldn't recommend it.