Search code examples
objective-cswifttypecheckingcustom-data-type

Storing Objective C custom model instance into swift class to id instance


I am using swift 4.0 and Objective C into my existing project. I have a Objective C custom model class DetailsModel and it has a property like

@interface DetailsModel : NSObject

@property (nonatomic, strong) id model;

And I am using the instance of DetailsModel into my swift class and storing another Objective C custom model class instance to above property, like

let model = DetailsModel()
model.model = item

This instance item if from another Objective C custom class CarModel

Now the problem is when I check type of above stored item

if type(of: current.model) == type(of: CarModel())

This fails and console just prints like Optional<Any>

So I cant identify the object type and perform necessary actions, I checked everything else is perfect.


Solution

  • please change

    if current.model is CarModel