Zaid Mustafa Zaid Mustafa -4 years ago 185
iOS Question

How can I store object has a multiple strings from firebase database using swift3

I have object named myArray in firebase database structure and it has a multiple lines of strings as [a , b and c ] like this My DB Structure how can I display this object in my Table View ,When I run it its coming in My Debug like this My Debug but its not appears in my simulator its appease like this My Simulator,I am using swift 3

This is My Code :

import UIKit
import Firebase

class TestTable: UITableViewController {

var arrayList = [test]()

var Ref = FIRDatabaseReference()


override func viewDidLoad() {
super.viewDidLoad()

Ref=FIRDatabase.database().reference()

Ref.child("Users").child("Test").observe(.childAdded ,with: { (snapshot) in

if let User = snapshot.value as? [String : AnyObject]{

let Add = test()

Add.setValuesForKeys(User)

self.arrayList.append(Add)


print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\(snapshot.value)")

self.tableView.reloadData()

}

})


}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return arrayList.count
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return arrayList[section].myArray.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as!TestTableCell

let hh = indexPath.section

cell.myTestView.text = (arrayList[hh].myArray as AnyObject) as? String

cell.titleText.text = arrayList[hh].title


return cell


}


}


and this is My Class :

class test : NSObject {


var myArray : NSArray = []

var title : String?

}

Answer Source

Your myArray is not array of string, it's dictionary. So if you want value of a,b and c in cell.myTestView.text. Then you need to run loop to get all a,b and c in cell.myTestView.text.

var string = ""

for (key, value) in arrayList[hh].myArray
{
    print("\(key) -> \(value)")
    string = string + "\n\(key) -> \(value)"
}

cell.myTestView.text = string

update your line cell.myTestView.text = (arrayList[hh].myArray as AnyObject) as? String with above code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download