2

i'm trying to get response in my model object, but facing an issue that it is showing me only first item of response, this is my code,

func getUserBalanceAPI()
{

    APIService.getUserBalance{ (responseObject) in
        if (responseObject?.status)! {
            self.balanceArray.removeAll()

             let user = UserCompleteBalance(JSON: (responseObject?.data as! [[String : Any]]).first!)
            self.balanceArray.append(user!)

            //Reload Collection View
            self.currencyCVC.reloadData()
        }
        else if !(responseObject?.status)! {
            Utilities.showBar(text: responseObject?.errorObject?.message)
        }
    }
}

How can i get all the items in an array? This is my response,

"responseBody": {
    "data": [
        {
            "auction_deposit": 4083.63,
            "currencyCode": "USD",
            "userCurrencyId": 1,
            "availableBalance": 64555.1,
            "currentBalance": 68638.73
        },
        {
            "auction_deposit": 0.0,
            "currencyCode": "AED",
            "userCurrencyId": 2,
            "availableBalance": 198000.0,
            "currentBalance": 198000.0
        },
        {
            "auction_deposit": 0.0,
            "currencyCode": "EUR",
            "userCurrencyId": 3,
            "availableBalance": 50000.0,
            "currentBalance": 50000.0
        }
    ]
}

This is my model class,

class UserCompleteBalance : Mappable {

var auctionDeposit : Int?
var availableBalance : Int?
var currencyCode : Int?
var currentBalance : Int?
var userCurrencyId : Int?

required init?(map: Map) {

}

func mapping(map: Map) {

    auctionDeposit <- map["auction_deposit"]
    currencyCode <- map["currencyCode"]
    userCurrencyId <- map["userCurrencyId"]
    availableBalance <- map["availableBalance"]
    currentBalance <- map["currentBalance"]

}
}

Now i want to store all the response in this.

2 Answers 2

1

Your issue is related to the fact that you are getting only the first dictionary on the dictionary array, so you need to loop over your dictionary array to convert each of them on UserCompleteBalance model

Updated

You need to use [weak self] and strongSelf inside closure to avoid retain cycles

try with this code

func getUserBalanceAPI()
{

    APIService.getUserBalance{ [weak self] (responseObject) in
       guard let strongSelf = self else {
           return 
       }

        if (responseObject?.status)! {
            strongSelf.balanceArray.removeAll()
            
             if let usersDataArray = responseObject?.data as? [[String : Any]] {
                for userData in usersDataArray {
                   strongSelf.balanceArray.append(UserCompleteBalance(JSON:userData))
                }
             }

            //Reload Collection View
            strongSelf.currencyCVC.reloadData()
        }
        else if !(responseObject?.status)! {
            Utilities.showBar(text: responseObject?.errorObject?.message)
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

yes this is perfect on, got it resolved thanks a lot. @Reinier Melian
Your welcome, @Hamza check my update about retain cycle, is important to avoid those before facing the real issue, happy coding
1

let user = UserCompleteBalance(JSON: (responseObject?.data as! [[String : Any]]).first!) self.balanceArray.append(user!)

Above lines should be in a loop through all objects in responseObject.data

Your getUserBalanceAPI functions should be

APIService.getUserBalance{ (responseObject) in
    if (responseObject?.status)! {
        self.balanceArray.removeAll()

        if let jsonObjects = responseObject?.data as? [[String : Any]] {
           for jsonObject in jsonObjects {
               if let user = UserCompleteBalance(JSON: jsonObject) {
                   self.balanceArray.append(user)
               }
           }
        }
        //Reload Collection View
        self.currencyCVC.reloadData()
    } else if !(responseObject?.status)! {
        Utilities.showBar(text: responseObject?.errorObject?.message)
    }
}

}

4 Comments

i'm trying to loop through it like this, for item in responseObject?.data { } but it is showing me error, Type 'AnyObject?' does not conform to protocol 'Sequence' . @jacob
Let's show my your class UserCompleteBalance. BTW, i dont see any loop in your code above. @Hamza
check my question i have updated with my model class and loop is in my comment. @jacob
An accepted answer or upvote will be a great motivation for me. Thanks. @Hamza

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.