8

How do I store multiple Types in a single array?

I'm doing the following:

var vehicles = [string]()
let mustang = Car() // car is a class name
mustang.brandName = "Ford"
mustang.modelName = "Mustang" // string type
mustang.modelYear = 1968
mustang.isConvertibible = true
mustang.isHatchback = false
mustang.hasSunroof = false // bool type
mustang.numberOfDoors = 2 // int type
mustang.powerSource = "gas engine"

// Add it to array
vehicles.append(mustang.brandName)
vehicles.append(mustang.modelName)
vehicles.append(mustang.modelYear) // Error: int type not supported
vehicles.append(mustang.isConvertibible) // Error: bool type not supported
vehicles.append(mustang.brandName)
vehicles.append(mustang.brandName)

How should I achieve this? I'm new to Swift / iOS.

1
  • 2
    Why can't you just store the car to the array, instead of each individual property ? ( ArrayThatholdsCars.append(mustang) ). If you need to store different type of object then you should declare you array type as AnyObject Commented Jul 15, 2016 at 5:11

4 Answers 4

10

Swift 5

Using protocol oriented programming

A little more swifty solution could be to use protocols. This is type safe, so you won't have to deal with unwrapping and casting optionals.

1. Define a protocol

protocol ModeOfTransportation {
    // Define anything in common between objects
    // Example:
    var color: UIColor { get set }
}

2. Make your models conform to the protocol

(The compiler will ensure you give it the correct types: ex. color)

struct Car: ModeOfTransportation {
    var color = UIColor.red
}

struct Bicycle: ModeOfTransportation {
    var color = UIColor.blue
}

3. Give your array the protocol as a type

class Example {
    var myTransportList: [ModeOfTransportation] = []

    func addModesOfTransport() {
        let car = Car()
        let bike = Bicycle()

        myTransportList.append(car)
        myTransportList.append(bike)
    }
}
Sign up to request clarification or add additional context in comments.

Comments

9

Instead of creating string array you can create the array of car like this and store directly car object

var cars = [Car]()
let mustang = Car() // car is a class name
mustang.brandName = "Ford"
mustang.modelName = "Mustang" // string type
mustang.modelYear = 1968
mustang.isConvertibible = true
mustang.isHatchback = false
mustang.hasSunroof = false // bool type
mustang.numberOfDoors = 2 // int type
mustang.powerSource = "gas engine"   
cars.append(mustang)  

Or if you want to store different types of object then you cant create array of AnyObject, So that it will store Any type of instance or object inside that array.

var arr = [AnyObject]()
let car = Car()
let bike = Bike()
arr.append(car)
arr.append(bike)

Comments

2

I think you're doing something really wrong.

Arrays are designed to hold only one type of stuff. If you really want to hold different types of stuff. Here's some possible methods:

  1. you can try creating an array of AnyObjects:

-

var vehicles = [AnyObject]()

This won't work if the type you want to store does not conform to AnyObject. If you have such a type, you will have to use Any.

  1. you can create an array of strings and convert all the values you want to store to a string

  2. You can just create an array of Cars to store all the properties of a car. Then you can add more cars to it later on. I think this is what you intended.

Comments

2

Per the Swift documentation:

AnyObject can represent an instance of any class type.

Any can represent an instance of any type at all, including function types.

So you could make an array of

var vehicles = [AnyObject]()

and this would take objects (class instances) of any type.

However, the documentation goes on to say:

Use Any and AnyObject only when you explicitly need the behavior and capabilities they provide. It is always better to be specific about the types you expect to work with in your code.

So, ideally you are specific about the type your array can hold.

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.