1 year ago

#55294

test-img

Bran

Swift fix Cannot assign value of type '()?' to type '[ShopData]?'

im stuck resolving my problem for over a week and i dont know how, im junior.

i want to get an array data from the dictionary data which both has the same name in backend = "data",

"data": {
    "current_page": 1,
    "data": [
        {
            "id_shop": "63ae163a-572a-4807-b5e7-5e8f91d9e18c",
            "name": "What's Next?",
            "slug": "whats-next",
            "address": null,
            "description": null,
            "logo": null
        },

and im write the model in IOS like this:

struct ShopData: {
var id_shop: String?
var name: String?
var slug: String?
var address: String?
var description: String?
var logo: String?

init(response: JSON) {
    id_shop = response["id_shop"].stringValue
    name = response["name"].stringValue
    slug = response["slug"].stringValue
    address = response["address"].stringValue
    description = response["description"].stringValue
    logo = response["logo"].stringValue
    
}

then im doing the method using alamofire and swiftyJson, and the error like this:

*response is the result response from API

if response!["status"].stringValue == "Success" {
                do {
                    let responses = JSON(response!)
                    let data = responses["data"]["data"]
                    shops = try self.shops?.append(ShopData(response: [data]))
                    
                    print(self.shops!)
                    
                } catch {
                    print("the error: \(error)")
                }      
            }

method & the error

error are on the append line, it said:

Cannot assign value of type '()?' to type '[ShopData]?'

i cant get any source that exactly the same

arrays

swift

append

alamofire

swifty-json

0 Answers

Your Answer

Accepted video resources