I have 4 global variables (String arrays) which i am appending in one view controller(say firstViewController). After appending values when i try to access them in another view controller a nil value is being passed
First ViewController
print("\(json)")
if let stockInfo = json?[0] {
if let value = stockInfo["l"] as? String {
print("Teja\(value)")
stockCurrentValue.append(value)
print("Teja\(stockCurrentValue)")
}
if let cp = stockInfo["cp"] as? String{
print("Teja\(cp)")
stockCp.append(cp)
print("Tea\(stockCp)")
}
if let c = stockInfo["c"] as? String{
print("Teja\(c)")
stockC.append(c)
}
if let pcls_fix = stockInfo["pcls_fix"] as? String{
print("Teja\(pcls_fix)")
stockPrevValue.append(pcls_fix)
}
}
The variables stockCurrentValue,stockC, stockCP, stockPrevValue are the four global variables i declared.
But when i try to access them in a new viewController
SecondViewController
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(selectedStockNames.count)
return selectedStockNames.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myStocks") as! StockCell
cell.stockNameCell.text = selectedStockNames[indexPath.row]
print("Teja123\(stockCurrentValue)")
stockValueFromGF = selectedStockValues[indexPath.row]
print("Teja123\(stockCp)")
return cell
}
So when i print StockCp value in firstViewContoller i get the value but when i try to print the same variable in seconViewController i get "[]" (nil value)
Use protocol and define it in the View Controller from where you want to set the values.
Use the method inside the function where the values of the global variables change.
Call the function and set the delegate in the view controller where you want to access the variables.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments