Search code examples
swiftalamofire

I am trying to append values to a string array


func getCategoryNames() {
  Alamofire.request(categoriesUrl).responseJSON { (response) in
        if ((response.result.value) != nil) {
             var jsonVar = response.result.value as! [String: Any]
             if let results = jsonVar["result"] as? [[String: Any]] {
                for result in results {
                    if let names = result["name"] as? String {
                         var tuy = [""]
                        tuy.append(names)

I am trying to put those value(names) inside tab(titleNames: tuy) But it is printing only the last element of the array Url is

let categoriesUrl = "https://cmsbmnc.agritechie.com/client/categories/"

I need the output like this tuy = ["ABC", "DEF","XYZ"]

let configure = SGPageTitleViewConfigure()
configure.indicatorStyle = .Default
configure.titleAdditionalWidth = 35
self.pageTitleView = SGPageTitleView(frame: CGRect(x: 0, y: pageTitleViewY, width: self.view.frame.size.width, height: 80), delegate: self, titleNames: tuy, configure: configure)
self.view.addSubview(self.pageTitleView!)

Solution

  • It is simple! Remove that string array var tuy = [""] from

    func getCategoryNames() {
      Alamofire.request(categoriesUrl).responseJSON { (response) in
            if ((response.result.value) != nil) {
                 var jsonVar = response.result.value as! [String: Any]
                 if let results = jsonVar["result"] as? [[String: Any]] {
                    for result in results {
                        if let names = result["name"] as? String {
                             var tuy = [""]
                            tuy.append(names)
    

    and declare it above the function.