title: How to create model and add Json response to model in swift?
body: <p>I need to create model for Json response, but i am unable to do</p>
  <p>how to create model and use that model variable in other viewcontroller</p>
  <pre><code> {
  &quot;jsonrpc&quot;: &quot;2.0&quot;,
  &quot;result&quot;: {
      &quot;userdata&quot;: {
          &quot;id&quot;: 47,
          &quot;fname&quot;: &quot;sample&quot;,
          &quot;lname&quot;: &quot;test&quot;,
          &quot;slug&quot;: &quot;sample-test&quot;,
          &quot;email&quot;: &quot;tom1@gmail.com&quot;,
          &quot;email_verified_at&quot;: &quot;2021-02-17 20:34:17&quot;,
      },
      &quot;token&quot;: &quot;eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjQ3LCJpc3MiOiJodHRwczovL2FwcGxlc2tvb2wuY29tL3ByZXZpZXcvYXBwbGVza29cyLCJuYmYiOjE2MTQwMTI3NzIsImp0aSI6Iktub2x4R3l2M2JEUVozUXAifQ.jjvMYGywY_85saSHPzEhvFD2__hGRa_AsbRj1S3rRXw&quot;
  }
  }
  </code></pre>
  <p>i have tried like below:</p>
  <pre><code>class LoginUserData: NSObject {
  
  var id: Int?
  var fname: String?
  var lname: String?
  var slug: String?
  var email: String?
  
  init(id: Int, fname: String, lname: String, slug: String, email: String) {
      
      self.id = id
      self.fname = fname
      self.lname = lname
      self.slug = slug
      self.email = email
  }
  }
  </code></pre>
  <p>but in loginservice i am unable to add data to model:</p>
  <pre><code>var loginData: LoginUserData?
  
  
  do {
  let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String:Any]
  print(&quot;the json output \(String(describing: json))&quot;)
  
  let error = json![&quot;error&quot;] as? [String : Any]
  
  let status = error?[&quot;status&quot;] as? [String : Any]
                  
  DispatchQueue.main.sync{
  if error != nil{
  }
  else{
      let res = json?[&quot;result&quot;] as? [String : Any]
      let uData = res?[&quot;userdata&quot;] as? [String : Any]
      
      self.loginData?.email = uData?[&quot;email&quot;] as? String
      
      print(&quot;nsoblject email: \(self.loginData?.email)&quot;) // here getting nil
      
  }
  }
  </code></pre>
  <p><code>self.loginData?.email = uData?[&quot;email&quot;] as? String</code> getting nil, how to use <code>loginData?.email</code> in other viewcontroller? please do help</p>