UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to access properties of a struct stored in an array when the array is of type [Any]

Forums > Swift

I have an array of type [Any]. I would like to be able to take an element of this array, check to see what its type this element is, then read or edit its properties. However, since the array is of type [Any] I am unable to access the properties of the elements like I would be able to if the array stored just one type.

Is there a way to do this?

Thanks!

2      

something like this or any thing else ?

var testArr: [Any] = [1, 2, 3, true, false]

for item in testArr {
    if item is Int {
        print(item)
    }
}

3      

You can do something like this:

let xs: [Any] = [
    1,
    "String",
    true,
]

xs.forEach {
    switch $0 {
    case let item as Int: print(item.magnitude)
    case let item as String: print(item.first!)
    case let item as Bool: print(item)
    default: break
    }
}
/*
1
S
true
*/

2      

Could this solve your problem?

struct Animal {
    let legs: Int
}

struct Vehicle {
    let wheels: Int
}

let dog = Animal(legs: 4)
let bike = Vehicle(wheels: 2)

let mixedArray: [Any] = [dog, bike]

for element in mixedArray {
    if let animal = element as? Animal {
        print("It's an animal with \(animal.legs) legs.")
    } else if let vehicle = element as? Vehicle {
        print("The vehicle has \(vehicle.wheels) wheels.")
    }
}

3      

@sodapool, @roosterboy, @AmitShrivastava,

Thanks for your replies. So I know now how to read the properties of struct elements in an [Any] array. However, I still am not sure how to write/edit those properties. Do you know how I could do that? Sorry if my original post wasn't clear.

I know a workaround solution, but I'm worried it may be much less efficient... I could check what type the array element is, then create a new instance of that type that has whatever new property values I'm looking for, then replace the old array element with the newly created instace.

I'm thinking if there were a way to edit the values of the original element's properties directly, that would be more efficient than creating a copy, editing the copy, then replacing the original with the edited copy. However, I'm definitely very in the dark as far as what kinds of operations are efficient and what kinds are significantly less efficient.

For my application, I'm looking to make LOTS of these kind of edits very often, so I'm anticipating that if I were to use a workaround solution that were significantly less efficient, that could end up being a problem.

Any thoughts, either about a solution, or about the efficiency of my workaround?

Thanks!

2      

Does your data have any similarities it conforms to? I don't think your Array stores really Any data. Perhaps you can create a Protocol with a set and a get method and your Array stores data with the type of this Protocol?

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.