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\$
9
  • \$\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\$ Commented May 7, 2017 at 9:35
  • \$\begingroup\$ Do any additional setup after loading the view for override viewDidLoad . apple xcode swift boilerplate code \$\endgroup\$ Commented May 7, 2017 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\$ Commented May 7, 2017 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\$ Commented May 7, 2017 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\$ Commented May 7, 2017 at 9:46

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.