1

I'm trying to make three arrays based on the JSON Data that I'm pulling from my restaurant. I want to have an array of Entre, Main, & Dessert that will display in a tableView Object. This is the code I'm using to pull in data:

func loadMeals() {
    Helpers.showActivityIndicator(activityIndicator, view)

    if let restaurantId = restaurant?.id {
        APIManager.shared.getMeals(restaurantId: restaurantId, completionHandler: { (json) in
            if json != nil {
                self.meals = []

                if let tempMeals = json["meals"].array {
                    for item in tempMeals {
                        let meal = Meal(json: item)
                        self.meals.append(meal)
                    }

                    self.tableView.reloadData()
                    Helpers.hideActivityIndicator(self.activityIndicator)
                }
            }
        })
    }
}

Item prints out:

items {
  "name" : "Spinach Artichoke",
  "course" : "entres",
  "short_description" : "savory",
  "id" : 20,
  "image" : "http:\/\/localhost:8000\/media\/product_images\/artichoke.jpg",
  "usage" : "homestyle",
  "sizes" : [
    {
      "size" : 3,
      "id" : 24,
      "product_id" : 20,
      "price" : 55.899999999999999
    },
    {
      "size" : 4,
      "id" : 25,
      "product_id" : 20,
      "price" : 78
    },
    {
      "size" : 5,
      "id" : 26,
      "product_id" : 20,
      "price" : 125
    }
  ]
}
items {
  "name" : "Pizza",
  "course" : "main",
  "short_description" : "Melty cheese",
  "id" : 19,
  "image" : "http:\/\/localhost:8000\/media\/product_images\/pizza.jpg",
  "usage" : "top",
  "sizes" : [
    {
      "size" : 6,
      "id" : 23,
      "product_id" : 19,
      "price" : 75.989999999999995
    }
  ]
}
items {
  "name" : "Chocolate Mousee Devil's cake",
  "course" : "dessert",
  "short_description" : "Sweet And Smooth",
  "id" : 18,
  "image" : "http:\/\/localhost:8000\/media\/product_images\/Devils_cake.jpg",
  "usage" : "sweets",
  "sizes" : [
    {
      "size" : 2,
      "id" : 20,
      "product_id" : 18,
      "price" : 50
    },
    {
      "size" : 3,
      "id" : 21,
      "product_id" : 18,
      "price" : 120
    },
    {
      "size" : 4,
      "id" : 22,
      "product_id" : 18,
      "price" : 376
    }
  ]
}

I'm trying to figure out how to create the arrays by pulling the data from this function. Any help would be appreciated.

1
  • What would you like your array to be exactly like? Can you give a format example? Commented May 14, 2018 at 17:13

1 Answer 1

0

Just You need to group By course

meals Array will be [[String:Any]] Key will be course Any will be Array of course items

  func loadMeals() {
         Helpers.showActivityIndicator(activityIndicator, view)

          if let restaurantId = restaurant?.id {
                    APIManager.shared.getMeals(restaurantId: restaurantId, completionHandler: { (json) in
                        if json != nil {
                            self.meals = []

                            if let tempMeals = json["meals"].array {
                               self.meals =  Dictionary(grouping: tempMeals, by: { $0["course"] as! String })
                                self.tableView.reloadData()
                                Helpers.hideActivityIndicator(self.activityIndicator)
                            }
                        }
                    })
                }
            }

OutPut Console:

["Main": [["name": Chocolate Mousee Devil's cake, "course": Main, "short_description": Sweet And Smooth, "id": 18, "image": http://localhost:8000/media/product_images/Devils_cake.jpg, "usage": sweets, "sizes": <__NSArrayI 0x60c0002456a0>(
{
    id = 20;
    price = 50;
    "product_id" = 18;
    size = 2;
},
{
    id = 21;
    price = 120;
    "product_id" = 18;
    size = 3;
},
{
    id = 22;
    price = 376;
    "product_id" = 18;
    size = 4;
}
)
]], "dessert": [["name": Chocolate Mousee Devil's cake, "course": dessert, "short_description": Sweet And Smooth, "id": 18, "image": http://localhost:8000/media/product_images/Devils_cake.jpg, "usage": sweets, "sizes": <__NSArrayI 0x60c000243d50>(
{
    id = 20;
    price = 50;
    "product_id" = 18;
    size = 2;
},
{
    id = 21;
    price = 120;
    "product_id" = 18;
    size = 3;
},
{
    id = 22;
    price = 376;
    "product_id" = 18;
    size = 4;
}
)
], ["name": Chocolate Mousee Devil's cake, "course": dessert, "short_description": Sweet And Smooth, "id": 18, "image": http://localhost:8000/media/product_images/Devils_cake.jpg, "usage": sweets, "sizes": <__NSArrayI 0x60c000248910>(
{
    id = 20;
    price = 50;
    "product_id" = 18;
    size = 2;
},
{
    id = 21;
    price = 120;
    "product_id" = 18;
    size = 3;
},
{
    id = 22;
    price = 376;
    "product_id" = 18;
    size = 4;
}
)
]]]
Sign up to request clarification or add additional context in comments.

10 Comments

I intended to use separators in the tableview to announce the specific kind of meal. Is it possible to do that if the array shows all items?
I split your data to be key and items do arrange your code to section table by course key
I'm trying this now but, "jsonDictionary" doesnt seem to be a valid key. I've imported swiftyJson but, it still doesn't seem to work
Sorry jsonDicyionary is my own I fix it check update
Thanks. Let me know when you've figured it out.
|

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.