1
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!)
1
  • You should place var tuy = [""] outside the for loop. You are emptying the array every iteration of the loop. Commented Jan 25, 2019 at 10:06

3 Answers 3

3

In each iteration of the loop you are creating a new tuy array.

You have to create the array once before the loop and declare it as regular empty array

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

or in a more convenient way

func getCategoryNames() {
  Alamofire.request(categoriesUrl).responseJSON { (response) in
        if let jsonVar = response.result.value as? [String: Any],
           let results = jsonVar["result"] as? [[String: Any]] {
                let tuy = results.compactMap { $0["name"] as? String }
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

You can declare variable of array is outside of function.

var tuy = [String]()

 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 {
                        tuy.append(names)
                   }
                }
          }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.