0

Here is my code:

import Foundation
import SwiftyJSON
import Alamofire

struct data {
    var users : User?
    
    init(json : JSON?) {
        
        if let value = json?["data"].dictionaryObject {
            
            let new = User(json: JSON(value))
            self.users = new
        }
    }
    

}
struct User {
    var userinfo : UserInfo?
    
    init(json : JSON?) {
        
        if let value = json?["user"].dictionaryObject {
            
            let new = UserInfo(json: JSON(value))
            self.userinfo = new
        }
    }
    
}

struct UserInfo {
    
    var id:String?
    var firstname: String?
    var username: String?
    var profilepicture: String?
    var biography: String?
    var lastname: String?
    var gender: String?
    var email: String?
    var phone: String?
    
    init(json : JSON?) {
        
        self.id = json?["id"].string
        self.firstname = json?["firstname"].string
        self.username = json?["username"].string
        self.profilepicture = json?["profilepicture"].string
        self.biography = json?["biography"].string
        self.lastname = json?["lastname"].string
        self.gender = json?["gender"].string
        self.email = json?["email"].string
        self.phone = json?["phone"].string
        
    }
}


 Alamofire.request(urlVal, method: .get, parameters:nil, encoding: URLEncoding.queryString, headers: headers).responseJSON { (resp) in
            switch resp.result {
            case .success(let value):
                let response = JSON(value)
                print("Response JSON: \(response)")
                let firstName = response["firstname"]
                let newUser = data(json: response)
                
                self.userData.append(newUser)
                print(self.userData)
                
            case .failure(let error):
                print(error)
                break
            }
RTXGamer
  • 3,215
  • 6
  • 20
  • 29
  • 4
    Your question is not clear. As I can see you have already created the model? What would you like to do now? – ZeMoon Sep 08 '21 at 05:41
  • 1
    have you searched for `Alamofire.request` on SO? There are plenty of examples to look at. Here is one I found at random https://stackoverflow.com/questions/27390656/how-to-return-value-from-alamofire – workingdog support Ukraine Sep 08 '21 at 06:29
  • Forget SwiftyJSON. Alamofire 5 adopted Codable to decode JSON directly into structs. And please name structs and classes with starting uppercase letter, but not `Data`. – vadian Sep 08 '21 at 07:26
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 13 '21 at 23:10

0 Answers0