Cannot add data to array inside read statement construct?

Hello all:

 

I am reading some data from an Azure SQL Server via an AppService, and I want to read the data into an array and then set a TableView to display the array of data. The code is pasted below, and I know the Names are being read correctly via the print statements but I cannot append the names to an array decalred outside the table.read scope. XCode tells me I must use “self”, but the array is empty when the loop exits. If I declare the array in the construct it works fine, but then I cannot access the array outside the scope of the loop, and so cannot set the tableview to display the data.

 

Any and all help is appreciated!

 

@IBOutlet weak var namesTableView: UITableView!

     var lastNames: [String] = []

    override func viewDidLoad()

    {

        super.viewDidLoad()

        let appDelegate = UIApplication.shared.delegate as! AppDelegate

        contactTable = appDelegate.client?.table(withName: “Contact”)

        var iNameCount : Int = 0

        contactTable?.read { (result, error) in

            if let err = error {

                print(“ERROR “, err)

            } else if let items = result?.items {

                for item in items {

                    //print(“LastName: “, NSString(data: item[“lastName”] as! Data, encoding:String.Encoding.utf8.rawValue))

                    var thelastname: String?

                    thelastname = item[“lastName”] as? String

                    let unwrappedln = thelastname!

                    print(unwrappedln)

                    self.lastNames.append(unwrappedln)

                    iNameCount += 1

                }

                print(“Names count: ” + String(iNameCount))

            }

        }

        print(self.lastNames)

        namesTableView.reloadData()

    }

Powered by WPeMatico

About

You may also like...

Comments are closed.