2
\$\begingroup\$

In my object Dish xxx.Dish, I want to access the Choice class price and name to display . dish data load from web API and I tested data loaded success full and put the data to the object dish and it return the object list to viewcontroller to load tableview.

Output of printed console

Optional([xxx.Dish, xxx.Dish])

and in the dish class before append optionList?.append(_obj)

xxx.DishOption

I am new to swift and is it right way to implement? Please suggest me?

class Dish {
        let dishId : String
        var optionList : [DishOption]?

        init?(fromAPIResponse resposne : Dictionary<String,AnyObject>) {

            guard let dishId = resposne["dishId"] as? String else {
                return nil

            }
            self.dishId = dishId
            if let objs =  resposne["options"] as?  [[String: AnyObject]]{
                optionList = [DishOption]()
                for obj in objs {
                    if  let _obj = DishOption(fromAPIResponse: obj){

                        optionList?.append(_obj)

                    }
                }
            }
        }

//another file

   class DishOption {

                let optionId : String

                var choiceList : [Choice]?

                init?(fromAPIResponse resposne : Dictionary<String,AnyObject>) {

                    guard let  optionId = resposne["optionId"] as? String else {
                        return nil
                    }

                    self.optionId = optionId

                    if let objs =  resposne["choices"] as?  [[String: AnyObject]]{
                        choiceList = [Choice]()
                        for obj in objs {
                            if  let _obj = Choice(fromAPIResponse: obj){

                                choiceList?.append(_obj)

                            }
                        }
                    }
                }
            }



 class Choice{
           let choiceId : String
                    let name : String
                    let price : String

                    init?(fromAPIResponse resposne : Dictionary<String,AnyObject>) {
                        guard let choiceId = resposne["choiceId"] as? String ,
                            let name = resposne["name"]  as? String,
                            let price = resposne["price"] as? String else {

                                return nil
                        }
                        self.choiceId = choiceId
                        self.name = name
                        self.price = price

                    }

            }

Usage:

     var  dishMenuList = [Dish]()
            guard let objs = json["menu_list"] as? [[String : AnyObject]] else {
                return
            }

            for obj in objs {

                if let _obj = Dish(fromAPIResponse: obj){

                 print(_obj.optionList) //always print nil  


                if let options = _obj.optionList {

                    for data in options {

                        print(data.displayAsButton)

                    }
                   }
                   dishMenuList.append(_obj)
                }

            }
\$\endgroup\$
  • \$\begingroup\$ Is this how you really indented your code, or did you have trouble posting it as intended? (The easiest way to post code is to paste it, highlight it, and press Ctrl-K to mark it as a code block.) \$\endgroup\$ – 200_success Mar 10 '17 at 7:38
  • \$\begingroup\$ Does this code work correctly as intended? It looks like your Stack Overflow question object list always print nil in swift, whose title suggests that it doesn't work. \$\endgroup\$ – 200_success Mar 10 '17 at 8:53
  • \$\begingroup\$ @200_success sound .. correct .. but i am not sure .. it is best practice or not ? so ask for review my code \$\endgroup\$ – cristan lika2 Mar 10 '17 at 9:37
  • \$\begingroup\$ @200_success please .. help me .. i have no team to do .. help me .. without stackoverflow .. and community \$\endgroup\$ – cristan lika2 Mar 10 '17 at 9:40
  • \$\begingroup\$ I have reopened the question, and rolled back to Rev 2 to conform to the How to Ask guidelines. \$\endgroup\$ – 200_success Mar 10 '17 at 9:42
1
\$\begingroup\$

1) i would not use inner classes.

2) i would extract the data extraction part from the init to a other class / helper - a class should only have the data structure and not how to convert it

3) typo resposne => response

\$\endgroup\$
  • \$\begingroup\$ could you make any sample for 2 )"i would extract the data extraction part from the init to a other class / helper - a class should only have the data structure and not how to convert it" i am new to swift i can't figure out how can i do that \$\endgroup\$ – cristan lika2 Mar 22 '17 at 6:19
1
\$\begingroup\$

1) Don't use nested classes. Classes should be in their own Swift files.

2) As previously stated, the data extraction should be in its own function/method rather than in the initializer.

\$\endgroup\$
  • \$\begingroup\$ could you make any sample for 2 )"As previously stated, the data extraction should be in its own function/method rather than in the initializer." i am new to swift i can't figure out how can i do that \$\endgroup\$ – cristan lika2 Mar 22 '17 at 6:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.