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!)
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.
The single task of appending the current value to the existing array is what is not working. I cannot find anything in the flow documentation regarding the Append to Array Variable action and syntax. I did some more messing around and found that the input is wanting the values WITHOUT square brackets.
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 }
I am looking for a way to concatenation/join the values of a column from a table to generate a single string of text Say I had a collection with 3 values in column Result: Result dog cat bird I would like to concatenate those values and using semi-colon as a delimiter to create a text value o
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) } } }
You can append the elements of one list to another with the "+" operator. a = [1, 2, 3] b = [10, 20] a = a + b print a # [1, 2, 3, 10, 20] If you want to append the lists and keep them as lists, then try: result = [] result.append(a) result.append(b) print result # [[1, 2, 3], [10, 20]]
Likewise, multiple values can also be appended using this method at the start of a JavaScript Array as we can append multiple values to the end of an array using the array.push() method. Other Different Ways For Javascript Elements To An Javascript Array
numpy.append - This function adds values at the end of an input array. The append operation is not inplace, a new array is allocated. Also the dimensions of the input arrays m
Basically, you have to take the array as input, convert it to a string, append your item and then convert the string back to any array object. Here's a Global Script example for a simple array: The script code is listed below to make it easier to copy and paste: var myArrayString = ""; // Initialise array string // Convert array object to array
Comments
- You should place var tuy = [""] outside the for loop. You are emptying the array every iteration of the loop.