快速追加数组值不显示外部语句

时间:2018-09-13 18:42:18

标签: arrays swift

在我的get调用中,我收到了一些值,并附加了var nameData = [String]()数组。 nameData数组值未显示外部语句。请检查我的以下代码

// Global Declarions
var nameData = [String]()

// GET Call values append
 if let content = result["contacts"] as? [[String:AnyObject]] {
            for category in content {

                self.nameData.append(category["name"] as! String)
            }
            print(self.nameData) // here I am getting
        }
    }
   self.addDropDown()
   print(self.nameData) // Empty array showing

现在,下面是我的下拉代码,在这里我想分配nameData数组

func addDropDown(){

   print(self.nameData) // Empty array showing why???

        // For Top textField
        let dropDownTop = VPAutoComplete()
        dropDownTop.dataSource =  self.nameData           -->// Here I need to assign nameData values if i do its showing empty values.
        dropDownTop.onTextField = emailTF
        dropDownTop.onView = self.view
        dropDownTop.show { (str, index) in
            print("string : \(str) and Index : \(index)")
            self.emailTF.text = str
        }
}

1 个答案:

答案 0 :(得分:0)

尝试一下:

if let content = result["contacts"] as? [[String:AnyObject]] {
        for category in content {
            self.nameData.append(category["name"] as! String)
        }
        DispatchQueue.main.async { self.addDropDown() } 
    }
}

在填充nameData数组后立即添加下拉列表。