0

Say I have a protocol:

protocol VehicleModel {...}

It is implemented by a number of different structs. (e.g. CarModel, TruckModel, etc.) I have a generic method to get the vehicle's 'model identifier'.

func modelIdentifierForVehicle<V: VehicleModel>(vehicleType: V.Type) -> String {
  return "\(vehicleType)"
}

If I call modelIdentifierForVehicle(CarModel.self) this returns "Car" just fine. But if I have a polymorphic collections of VehicleModel's and I try to call modelIdentifierForVehicle(model.dynamicType) on each of them, Xcode says "Cannot invoke 'modelIdentifierForVehicle' with argument list of type (VehicleModel.Type)" Why is this? And how can I work around it?

2
  • Can you post the relevant code inside of VehicleModel and modelIdentifierForVehicle? Commented Aug 25, 2015 at 9:30
  • @ABakerSmith Sure I updated the post. Basically it makes a unique key for the vehicle type because types themselves are not Hashable. Commented Aug 25, 2015 at 12:36

1 Answer 1

1

Since you're only converting vehicleType to a String in modelIdentifierForVehicle, I would argue why you need to use constrain V to VehicleModel, or even use generics at all:

func typeIdentifier(t: Any.Type) -> String {
    return "\(t)"
}  

let vehicles: [VehicleModel.Type] = [CarModel.self, TruckModel.self]
typeIdentifier(vehicles[0]) // CarModel

If there's a reason you need use a VehicleModel, assuming VehicleModel doesn't use Self or associated type requirements, you could do:

func modelIdentifierForVehicle(vehicleType: VehicleModel.Type) -> String {
    return "\(vehicleType)"
}

If you're using Swift 2, you could instead use a protocol extension:

extension VehicleModel {
    static var modelIdentifier: String {
        return "\(self.dynamicType)"
    }
}

// The array from earlier.
vehicles[1].modelIdentifier // TruckModel.Type
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.