Skip to main content
Notice removed Authoritative reference needed by CommunityBot
Bounty Ended with Justin Miller's answer chosen by CommunityBot
added an apparently missing comment symbol so the line no longer looks like improper code.
Source Link
Gero
  • 4.4k
  • 26
  • 38
     for currentCategory in mainCategories {
            

    // guard against if there are child categories
            guard var children = currentCategory.children, children.count > 0 else {
                //  Save the context
                self.coreData.saveStore()
                continue
            }                    
            for thisChildCategory in children {
                
                if thisChildCategory.children?.count > 0 {
                    for innerChildCategory in thisChildCategory.children! {
                        print("innerChildCategory name \(String(describing: innerChildCategory.name))")
                    }
                }
                
                if let child = thisChildCategory.children {
                    children = child
                }
                
                //  Create new object
                if let currentChildCategory  = self.coreData.insertNewObject(CoreDataEntities.BijouCategories.rawValue,
                    keyValues: ["id" : thisChildCategory.id! as Optional<AnyObject>,
                        "uuid" : thisChildCategory.uuid as Optional<AnyObject>,
                        "name" : thisChildCategory.name! as Optional<AnyObject>,
                        "gender" : thisChildCategory.gender as Optional<AnyObject>!,
                        "active" : NSNumber(value: false)]) as? BijouCategories {
                    
                    //  Set as parent category
                    currentChildCategory.parentCategory = parentCategory
                    

                    //  Save the context
                    self.coreData.saveStore()
                    
                }
            }
            
        }
     for currentCategory in mainCategories {
            

    guard against if there are child categories
            guard var children = currentCategory.children, children.count > 0 else {
                //  Save the context
                self.coreData.saveStore()
                continue
            }                    
            for thisChildCategory in children {
                
                if thisChildCategory.children?.count > 0 {
                    for innerChildCategory in thisChildCategory.children! {
                        print("innerChildCategory name \(String(describing: innerChildCategory.name))")
                    }
                }
                
                if let child = thisChildCategory.children {
                    children = child
                }
                
                //  Create new object
                if let currentChildCategory  = self.coreData.insertNewObject(CoreDataEntities.BijouCategories.rawValue,
                    keyValues: ["id" : thisChildCategory.id! as Optional<AnyObject>,
                        "uuid" : thisChildCategory.uuid as Optional<AnyObject>,
                        "name" : thisChildCategory.name! as Optional<AnyObject>,
                        "gender" : thisChildCategory.gender as Optional<AnyObject>!,
                        "active" : NSNumber(value: false)]) as? BijouCategories {
                    
                    //  Set as parent category
                    currentChildCategory.parentCategory = parentCategory
                    

                    //  Save the context
                    self.coreData.saveStore()
                    
                }
            }
            
        }
     for currentCategory in mainCategories {
            

    // guard against if there are child categories
            guard var children = currentCategory.children, children.count > 0 else {
                //  Save the context
                self.coreData.saveStore()
                continue
            }                    
            for thisChildCategory in children {
                
                if thisChildCategory.children?.count > 0 {
                    for innerChildCategory in thisChildCategory.children! {
                        print("innerChildCategory name \(String(describing: innerChildCategory.name))")
                    }
                }
                
                if let child = thisChildCategory.children {
                    children = child
                }
                
                //  Create new object
                if let currentChildCategory  = self.coreData.insertNewObject(CoreDataEntities.BijouCategories.rawValue,
                    keyValues: ["id" : thisChildCategory.id! as Optional<AnyObject>,
                        "uuid" : thisChildCategory.uuid as Optional<AnyObject>,
                        "name" : thisChildCategory.name! as Optional<AnyObject>,
                        "gender" : thisChildCategory.gender as Optional<AnyObject>!,
                        "active" : NSNumber(value: false)]) as? BijouCategories {
                    
                    //  Set as parent category
                    currentChildCategory.parentCategory = parentCategory
                    

                    //  Save the context
                    self.coreData.saveStore()
                    
                }
            }
            
        }
Notice added Authoritative reference needed by Rani
Bounty Started worth 50 reputation by Rani
Source Link
Rani
  • 3.5k
  • 13
  • 53
  • 89

Nested repeatative loop in swift

I am trying to parse a nested iterative loop in swift I am getting the response from web service in the following format

{
    "categories": [{
        "name": "Default Category",
        "id": "default_category",
        "children": [{
            "uuid": "783f491fef5041438fb7a2c3bf6a3650",
            "name": "Accessories",
            "children": [{
                "uuid": "d21b4491ff784a9bae88de279b99fac3",
                "name": "All Accessories",
                "children": [{
                        "uuid": "2b1a23c4107844ad8a7afc1b324d0ffd",
                        "name": "Belts",
                        "children": [{
                                "uuid": "2b1a23c4107844ad8a7afc1b324d0ffd",
                                "name": "Belts",
                                "children": []

                            },
                            {
                                "uuid": "2b1a23c4107844ad8a7afc1b324d0ffd",
                                "name": "Belts",
                                "children": []
                            }
                        ]
                    },
                    {
                        "uuid": "a1c2a64c36c2461cad3d5f850e4fd0f5",
                        "name": "Hats",
                        "children": []
                    },
                    {
                        "uuid": "8f26bc764b8342feaa0cb7f3b96adcae",
                        "name": "Scarves",
                        "children": []
                    },
                    {
                        "uuid": "aa1116d1a0254ecea836cc6b32eeb9e0",
                        "name": "Sunglasses",
                        "children": []
                    },
                    {
                        "uuid": "9d7033233e8f47eaa69eb1aaf2e98cdd",
                        "name": "Watches",
                        "children": []
                    }
                ]
            }]
        }],
        "uuid": "6a23415771064e7aaad59f84f8113561"
    }]
}

Inside, the categories, there is 'children' key which in turn can contain another children and so on. I want to continuously loop inside the children key until the children key is empty and insert the last child into database.

Following is the code which i have done

     for currentCategory in mainCategories {
            

    guard against if there are child categories
            guard var children = currentCategory.children, children.count > 0 else {
                //  Save the context
                self.coreData.saveStore()
                continue
            }                    
            for thisChildCategory in children {
                
                if thisChildCategory.children?.count > 0 {
                    for innerChildCategory in thisChildCategory.children! {
                        print("innerChildCategory name \(String(describing: innerChildCategory.name))")
                    }
                }
                
                if let child = thisChildCategory.children {
                    children = child
                }
                
                //  Create new object
                if let currentChildCategory  = self.coreData.insertNewObject(CoreDataEntities.BijouCategories.rawValue,
                    keyValues: ["id" : thisChildCategory.id! as Optional<AnyObject>,
                        "uuid" : thisChildCategory.uuid as Optional<AnyObject>,
                        "name" : thisChildCategory.name! as Optional<AnyObject>,
                        "gender" : thisChildCategory.gender as Optional<AnyObject>!,
                        "active" : NSNumber(value: false)]) as? BijouCategories {
                    
                    //  Set as parent category
                    currentChildCategory.parentCategory = parentCategory
                    

                    //  Save the context
                    self.coreData.saveStore()
                    
                }
            }
            
        }

But this is not saving all the last child category in database.