3
\$\begingroup\$

My current code working perfectly, it is for data load from web api to the UIViewController (UITableView).
But I am confused that if it violates the Dependency Inversion Principle(DIP) of SOLID?
If so, how to fix that?

class AreaController: UIViewController {

        var townCityList : [TownCity]?

        override func viewDidLoad() {
            super.viewDidLoad()

           // locations implements ILocation interface

            let service = NetworkModelTownCity()


            // So here, when creating a new instance you're injecting the dependecy.

            let dataprocess = DataProcessLocation(location : service)


            dataprocess.locations({ (locations) in

                 self.townCityList = locations
                 self.tableView.reloadData()
            })
        }

    }

protocol Ilocation {

    func townCitys(_ completionHandler: @escaping (_ locations: [TownCity]) -> Void)
}

class DataProcessLocation {

    var location : Ilocation!

    init(location : Ilocation) {

       self.location = location
    }

    func locations(_ completionHandler: @escaping (_ locations: [TownCity]) -> Void) {

        self.location.townCitys( { (locations) in

            completionHandler(locations)

        })

    }


}

class NetworkModelTownCity : Ilocation {


     func townCitys(_ completionHandler: @escaping ([TownCity]) -> Void) {

        let _jsonFormatString = ["":""]

        RestApiService.shared.fetchDataByPostRequest(with: _jsonFormatString  , actionMethod: Constants.ApiRequest.actionMethod.townCity.rawValue) { (json) in

            var  _objs = [TownCity]()

            print(json)

            guard let objList = json as? [AnyObject] else {
                return
            }

            for obj in objList {
                if  let _obj = TownCity(fromAPIResponse: obj as! Dictionary<String, AnyObject>){
                    _objs.append(_obj)
                }

            }


            completionHandler(_objs)

        }
    }

}

class TownCity{

    var  townId : String = ""
    var  cityId : String = ""
    var  townName : String = ""
    var selectedShopTypeId : String = ""

    init?(fromAPIResponse resposne : Dictionary<String,AnyObject>){

        guard let townId = resposne["townId"]  as? String , let cityId = resposne["cityId"] as? String , let townName = resposne["townName"] as? String  else {  
            return nil
        }

        self.townId = townId
        self.cityId = cityId
        self.townName = townName
    }
}
\$\endgroup\$
  • \$\begingroup\$ just some general comments: why override viewDidLoad if you are simply calling the superclass? doesn't make sense. also i would inject DataProcessLocation as a parameter when instantiating the areaController which means you won't have a need to instantiate NetworkModelTownCity in your controller. \$\endgroup\$ – BKSpurgeon May 7 '17 at 9:35
  • \$\begingroup\$ Do any additional setup after loading the view for override viewDidLoad . apple xcode swift boilerplate code \$\endgroup\$ – Nazmul Hasan May 7 '17 at 9:40
  • \$\begingroup\$ @t3chb0t Load data from WebAPI to the ViewController it is working fine no issue or problem but i need concept SOLID (DIP) \$\endgroup\$ – Nazmul Hasan May 7 '17 at 9:42
  • \$\begingroup\$ Please do not change the title. The original one violates the site rules. It should tell what the code does and not be your quesiton about the code. \$\endgroup\$ – t3chb0t May 7 '17 at 9:45
  • \$\begingroup\$ @t3chb0t could you explain please why you remove my problem .. if you provide this Load data from WebAPI to the ViewController then no issue in my code \$\endgroup\$ – Nazmul Hasan May 7 '17 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.