Shimaa Magdi
Shimaa Magdi

Reputation: 31

IOS: Unrecognized selector sent to instance Swift class in Objective C

I Know it's known exception but i can't find a solution

I have class ViewModel.swift shared between objective-c View controller and swift viewController I'm trying to pass it as a parameter when creating each view controller.. It works fine in the swift VC but crashes in the objective c one.

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@class CarsListViewModel;
@class CarData;

@interface MapViewController : UIViewController
@property (nonatomic, strong) IBOutlet MKMapView *mapView;
@property (nonatomic, retain) CarsListViewModel *viewModel;
+(MapViewController*)createWithViewModel:(CarsListViewModel*)viewModel;
@end


@implementation MapViewController
@synthesize mapView;
@synthesize viewModel;

+(MapViewController*)createWithViewModel:(CarsListViewModel*)viewModel {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:MAIN_STORYBOARD bundle:nil];
    MapViewController * mapViewController = (MapViewController *)[sb instantiateViewControllerWithIdentifier:MAP_VC_Identifier];
    mapViewController.viewModel = viewModel;
    return mapViewController;
}

And Here the swift class

import RxSwift
import RxCocoa
import CoreLocation

@objc
class CarsListViewModel: NSObject {

@objc var carsList = [CarData]()

var selectedBounds = Variable(Places.hamburgBounds)

lazy var data: Driver<[CarData]> = {
    return self.selectedBounds.asObservable()
        .throttle(0.3, scheduler: MainScheduler.instance)
        .flatMapLatest({ (bounds) -> Observable<[CarData]>  in
            return NetworkManager.shared.fetchPlacemarks(forBounds: bounds).flatMapLatest{ Observable.just( $0.poiList ) }
        })
        .asDriver(onErrorJustReturn: [])
}()

}

I'm getting this error Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController setViewModel:]: unrecognized selector sent to instance 0x102015920'

Upvotes: 0

Views: 1008

Answers (1)

Shimaa Magdi
Shimaa Magdi

Reputation: 31

The problem was in the viewController casting, it works fine after unchecked inherit module from target Storyboard class

Upvotes: 1

Related Questions