title: Swift : Separate classes for network requests
body: <p>friends!, I am new to Swift. I need to make multiple API requests in one of my view controllers. If I put all the code in ViewController it will be messy.
  So I am trying to develop this simple architecture to separate the concepts but I'm not sure whether it's the best approach to handle this situation.</p>
  
  <pre><code>/*-----------------------------------------------------*/
  /* RestClient.swift */
  /*-----------------------------------------------------*/
  protocol RestClientDelegate {
      func dataDidLoad(json:String)
  }
  class RestClient {
  
      var delegate:RestClientDelegate
  
      //Handles all the network codes and exceptions
      func makeGetRequest(url){
          //send a get request to the server  
          //on error - handle erros 
          //on success - pass Json response to HttpUser class
          delegate.dataDidLoad(jsonOutput)
      }
  
  }
  
  /*-----------------------------------------------------*/
  /* HttpUser.swift */
  /*-----------------------------------------------------*/
  protocol UserDelegate(){
      func usersDidLoad(usersArray:[User])//User object array
  }
  class HttpUser, RestClientDelegate {
  
      var delegate:UserDelegate
  
      func getUsers(){
          //Use rest client to make an api call
          var client = RestClient()
          client.delegate = self
          client.makeGetRequest("www.example.com/users")
      }
  
      //Once RestClient get the json string output, it will pass to
      //this function
      func dataDidLoad(json:String){
          //parse json - Handles json exceptions
          //populate user objects in an array
          //pass user array to the ViewController
          delegate.usersDidLoad(usersArray:[User])
      }
  
  }
  
  /*-----------------------------------------------------*/
  /* UserViewController.swift */
  /*-----------------------------------------------------*/
  class UserViewController:UIViewController, UserDelegate {
  
      override viewDidLoad(){
          super.viewDidLoad()
  
          //Ask http user class to retrieve users
          var httpUser = HttpUser()
          httpUser.delegate = self
          httpUser.getUsers()
      }
  
      //Callback function to get users array
      func usersDidLoad(usersArray:[User]) {
          //now you have users object array
          //populate a table view
      }
  
  }
  </code></pre>
  
  <ol>
  <li>RestClient.swift - Make the API request and pass a json output. This class contains all the code related to network <strong>GET/POST</strong> requests. I can modify this class in the future without affecting other classes.</li>
  <li>HttpUser.swift - Get json output make an Users Array and pass it. This class does not care about network requests. It will just <strong>handle the JSON response</strong> and parse it into an object array. I will be having multiple of these. (ex: HttpBlogs, HttpComments)</li>
  <li>UserViewController.swift - Get Users Array and populate a table view. This will handle only <strong>sections related to the UI</strong>.</li>
  </ol>
  
  <p>Can you tell me is this approach is good to follow?
  Is there a much better way to achieve this?</p>
  
  <p>Thanks a lot everyone!</p>
  
  <p>-Please note: I don't want to use 3rd party libraries like Alamofire in this case.</p>