Search code examples
iosswiftstructpublicalamofire

How to make my Struct variable public?


My question looks very simple. But i am struggling it for a day.

I need to declare my struct variable as a public variable. So that the UICollectionViewController Data methods can access the Struct Variable.

How can i do it ? I tried it by myself. But i can't achieve it

class SkillsController: UICollectionViewController {

    var mcnameArray :[String] = []

    var mcidArray :[String] = []


    func getSkills(){
            Alamofire.request(.GET, "http://www.wols.com/index.php/capp/main_category_list")
                .responseJSON { (_, _, data, _) in
                    let json = JSON(data!)
                    let Count = json.count
                    for index in 0...Count-1 {
                        var ds = json[index]["DISPLAY_STATUS"].string
                        if ds == "Y" {
                            var mcname = json[index]["MAIN_CATEGORY_NAME"].string
                            self.mcnameArray.append(mcname!)
                            var mcid = json[index]["MAIN_CATEGORY_ID"].string
                            self.mcidArray.append(mcid!)
                        }

                    }
                    var skill = Skills(mcname: self.mcnameArray, mcid: self.mcidArray)
                    println(skill.mcname.count)
            }

        }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            //#warning Incomplete method implementation -- Return the number of items in the section
            return skill.mcname.count// Throws an Error
        }

Solution

  • Your variable is not "private" but rather it's locally scoped to the function where it's defined.

    If you move it next to var mcnameArray and var mcidArray, then it will be accessible from the other functions too.

    However, since you are making an asynchronous network request, the variable will need to change. You might want to make it optional (var skill: Skill?) so that it can be nil at the beginning, and change later.