Can't view dictionary data from Firebase
I had a working solution before, but I couldn't find a way to delete an entry on Firebase. The old solution only deleted entries on the local device(only the "list" and "desc" arrays would delete from the local device, but not the database). So I added the "post" variable to keep each title and description tied together with a unique ID, so I may retrieve that ID to delete the entry from the database.
Now using the "post" variable, the title and description uploads to Firebase but I'm having trouble retrieving and displaying data that I posted on firebase on my UITableView. I get an error when I try to append snapshot data into my dictionary named "post".
An error occurs at this line:
if let itemArray = snapshot.value as? [[String : Optional <String>]]
{
post.append(itemArray) \ERROR: Value of type '[String : Optional<String>]' has no member 'append'
self.myTableView.reloadData()
}
}
Here is the full code below
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
//******THIS IS THE TABLEVIEWCONTROLLER******
@IBOutlet weak var myTableView: UITableView! // link to tableview item in storyboard
//required function when using tableview; wants to know how many cells to produce (takes template from prototype cell in storyboard)
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return(list.count)
}
// required function when using tableview; actual link to each cell from storyboard
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = list[indexPath.row]
return (cell)
}
//function allows you to swipe left to delete an entry(cell)
public func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath){
if editingStyle == UITableViewCell.EditingStyle.delete
{
var key = [String]()
ref?.child(key[indexPath.row]).setValue(nil) // removes a entry on Firebase using its unique ID
key.remove(at: indexPath.row)
list.remove(at: indexPath.row)
desc.remove(at: indexPath.row)
myTableView.reloadData()
}
}
override func viewDidAppear(_ animated: Bool) {//change "viewDidLoad()" back to "viewDidAppear()"
ref = Database.database().reference() // sets the variable "ref" to connect to our Firebase database
list.removeAll() // Deletes all older data, so only data thats on the Firebase Database will be added to the "list" array
desc.removeAll() // Deletes all older data, so only data thats on the Firebase Database will be added to the "desc" array
let alert = UIAlertController(title: "Enter Details for Your Post", message: "", preferredStyle: UIAlertController.Style.alert)
let titleField = alert.textFields![0] as UITextField
let descField = alert.textFields![1] as UITextField
let key = ref?.child("task").childByAutoId().key
let post = ["uid": key, // Gets auto generated ID for database entry
"title": titleField.text, // Saves the title field to Firebase
"description": descField.text] // Saves the description field to Firebase
handle = ref?.child("task").observe(.childAdded, with: { (snapshot) in // looks for each title in the tasks'title directory in the Firebase database, so it may display them on the tableview
if let itemArray = snapshot.value as? [[String : Optional <String>]]
{
post.append(itemArray)
self.myTableView.reloadData()
}
}
)}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
myIndex = indexPath.row
performSegue(withIdentifier: "detailsegue", sender: self)
}
@IBAction func quickAdd(_ sender: UIButton) // Link to add button on FirstViewController
{
uploadToFirebase(title: "Post the task?", message: "Enter a title and description for your task")
}
func uploadToFirebase (title: String, message: String)
{
let alert = UIAlertController(title: "Enter Details for Your Post", message: "", preferredStyle: UIAlertController.Style.alert)
alert.addTextField(configurationHandler: { (titleField : UITextField!) in // Adds a textfield to the alert
titleField.placeholder = "Enter item"
})
alert.addTextField(configurationHandler: { (descField : UITextField!) in // Adds a textfield to the alert
descField.placeholder = "Enter Description"
})
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.default, handler: { // Adds cancel button to alert; voids whatever has been inputted
(action : UIAlertAction!) -> Void in })
let saveAction = UIAlertAction(title: "Save", style: UIAlertAction.Style.default, handler: { (action : UIAlertAction! ) in // Adds save button to alert; saves the tasks to Firebase if clicked
let titleField = alert.textFields![0] as UITextField
let descField = alert.textFields![1] as UITextField
if (titleField.text != "") // Checks to make sure titleField isn't empty
{
if (descField.text != "") // Checks to make sure descField isn't empty
{
let key = ref?.child("task").childByAutoId().key
let post = ["uid": key, // Gets auto generated ID for database entry
"title": titleField.text, // Saves the title field to Firebase
"description": descField.text] // Saves the description field to Firebase
ref?.child("task").child(key!).setValue(post) // Saves the task for Firebase, ties each post with a unique ID
}
}
})
alert.addAction(saveAction)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil) // Presents the alert on screen
}
}
Here is my Firebase Database. The data under the child "task" doesn't appear on the UITableView
{
"task" : {
"-LT4Xtdo7Z0oyqHXHgif" : {
"description" : "4:02 desc",
"title" : "4:02 test",
"uid" : "-LT4Xtdo7Z0oyqHXHgif"
},
"-LU6eMyaKN9DZF-YYiLU" : {
"description" : "You Know!",
"title" : "HU",
"uid" : "-LU6eMyaKN9DZF-YYiLU"
},
swift
add a comment |
I had a working solution before, but I couldn't find a way to delete an entry on Firebase. The old solution only deleted entries on the local device(only the "list" and "desc" arrays would delete from the local device, but not the database). So I added the "post" variable to keep each title and description tied together with a unique ID, so I may retrieve that ID to delete the entry from the database.
Now using the "post" variable, the title and description uploads to Firebase but I'm having trouble retrieving and displaying data that I posted on firebase on my UITableView. I get an error when I try to append snapshot data into my dictionary named "post".
An error occurs at this line:
if let itemArray = snapshot.value as? [[String : Optional <String>]]
{
post.append(itemArray) \ERROR: Value of type '[String : Optional<String>]' has no member 'append'
self.myTableView.reloadData()
}
}
Here is the full code below
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
//******THIS IS THE TABLEVIEWCONTROLLER******
@IBOutlet weak var myTableView: UITableView! // link to tableview item in storyboard
//required function when using tableview; wants to know how many cells to produce (takes template from prototype cell in storyboard)
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return(list.count)
}
// required function when using tableview; actual link to each cell from storyboard
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = list[indexPath.row]
return (cell)
}
//function allows you to swipe left to delete an entry(cell)
public func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath){
if editingStyle == UITableViewCell.EditingStyle.delete
{
var key = [String]()
ref?.child(key[indexPath.row]).setValue(nil) // removes a entry on Firebase using its unique ID
key.remove(at: indexPath.row)
list.remove(at: indexPath.row)
desc.remove(at: indexPath.row)
myTableView.reloadData()
}
}
override func viewDidAppear(_ animated: Bool) {//change "viewDidLoad()" back to "viewDidAppear()"
ref = Database.database().reference() // sets the variable "ref" to connect to our Firebase database
list.removeAll() // Deletes all older data, so only data thats on the Firebase Database will be added to the "list" array
desc.removeAll() // Deletes all older data, so only data thats on the Firebase Database will be added to the "desc" array
let alert = UIAlertController(title: "Enter Details for Your Post", message: "", preferredStyle: UIAlertController.Style.alert)
let titleField = alert.textFields![0] as UITextField
let descField = alert.textFields![1] as UITextField
let key = ref?.child("task").childByAutoId().key
let post = ["uid": key, // Gets auto generated ID for database entry
"title": titleField.text, // Saves the title field to Firebase
"description": descField.text] // Saves the description field to Firebase
handle = ref?.child("task").observe(.childAdded, with: { (snapshot) in // looks for each title in the tasks'title directory in the Firebase database, so it may display them on the tableview
if let itemArray = snapshot.value as? [[String : Optional <String>]]
{
post.append(itemArray)
self.myTableView.reloadData()
}
}
)}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
myIndex = indexPath.row
performSegue(withIdentifier: "detailsegue", sender: self)
}
@IBAction func quickAdd(_ sender: UIButton) // Link to add button on FirstViewController
{
uploadToFirebase(title: "Post the task?", message: "Enter a title and description for your task")
}
func uploadToFirebase (title: String, message: String)
{
let alert = UIAlertController(title: "Enter Details for Your Post", message: "", preferredStyle: UIAlertController.Style.alert)
alert.addTextField(configurationHandler: { (titleField : UITextField!) in // Adds a textfield to the alert
titleField.placeholder = "Enter item"
})
alert.addTextField(configurationHandler: { (descField : UITextField!) in // Adds a textfield to the alert
descField.placeholder = "Enter Description"
})
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.default, handler: { // Adds cancel button to alert; voids whatever has been inputted
(action : UIAlertAction!) -> Void in })
let saveAction = UIAlertAction(title: "Save", style: UIAlertAction.Style.default, handler: { (action : UIAlertAction! ) in // Adds save button to alert; saves the tasks to Firebase if clicked
let titleField = alert.textFields![0] as UITextField
let descField = alert.textFields![1] as UITextField
if (titleField.text != "") // Checks to make sure titleField isn't empty
{
if (descField.text != "") // Checks to make sure descField isn't empty
{
let key = ref?.child("task").childByAutoId().key
let post = ["uid": key, // Gets auto generated ID for database entry
"title": titleField.text, // Saves the title field to Firebase
"description": descField.text] // Saves the description field to Firebase
ref?.child("task").child(key!).setValue(post) // Saves the task for Firebase, ties each post with a unique ID
}
}
})
alert.addAction(saveAction)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil) // Presents the alert on screen
}
}
Here is my Firebase Database. The data under the child "task" doesn't appear on the UITableView
{
"task" : {
"-LT4Xtdo7Z0oyqHXHgif" : {
"description" : "4:02 desc",
"title" : "4:02 test",
"uid" : "-LT4Xtdo7Z0oyqHXHgif"
},
"-LU6eMyaKN9DZF-YYiLU" : {
"description" : "You Know!",
"title" : "HU",
"uid" : "-LU6eMyaKN9DZF-YYiLU"
},
swift
You are setting “post” as a dictionary. Dictionary items cannot be added with append. They need a [key,value] pair.
– Galo Torres Sevilla
Dec 20 '18 at 21:52
add a comment |
I had a working solution before, but I couldn't find a way to delete an entry on Firebase. The old solution only deleted entries on the local device(only the "list" and "desc" arrays would delete from the local device, but not the database). So I added the "post" variable to keep each title and description tied together with a unique ID, so I may retrieve that ID to delete the entry from the database.
Now using the "post" variable, the title and description uploads to Firebase but I'm having trouble retrieving and displaying data that I posted on firebase on my UITableView. I get an error when I try to append snapshot data into my dictionary named "post".
An error occurs at this line:
if let itemArray = snapshot.value as? [[String : Optional <String>]]
{
post.append(itemArray) \ERROR: Value of type '[String : Optional<String>]' has no member 'append'
self.myTableView.reloadData()
}
}
Here is the full code below
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
//******THIS IS THE TABLEVIEWCONTROLLER******
@IBOutlet weak var myTableView: UITableView! // link to tableview item in storyboard
//required function when using tableview; wants to know how many cells to produce (takes template from prototype cell in storyboard)
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return(list.count)
}
// required function when using tableview; actual link to each cell from storyboard
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = list[indexPath.row]
return (cell)
}
//function allows you to swipe left to delete an entry(cell)
public func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath){
if editingStyle == UITableViewCell.EditingStyle.delete
{
var key = [String]()
ref?.child(key[indexPath.row]).setValue(nil) // removes a entry on Firebase using its unique ID
key.remove(at: indexPath.row)
list.remove(at: indexPath.row)
desc.remove(at: indexPath.row)
myTableView.reloadData()
}
}
override func viewDidAppear(_ animated: Bool) {//change "viewDidLoad()" back to "viewDidAppear()"
ref = Database.database().reference() // sets the variable "ref" to connect to our Firebase database
list.removeAll() // Deletes all older data, so only data thats on the Firebase Database will be added to the "list" array
desc.removeAll() // Deletes all older data, so only data thats on the Firebase Database will be added to the "desc" array
let alert = UIAlertController(title: "Enter Details for Your Post", message: "", preferredStyle: UIAlertController.Style.alert)
let titleField = alert.textFields![0] as UITextField
let descField = alert.textFields![1] as UITextField
let key = ref?.child("task").childByAutoId().key
let post = ["uid": key, // Gets auto generated ID for database entry
"title": titleField.text, // Saves the title field to Firebase
"description": descField.text] // Saves the description field to Firebase
handle = ref?.child("task").observe(.childAdded, with: { (snapshot) in // looks for each title in the tasks'title directory in the Firebase database, so it may display them on the tableview
if let itemArray = snapshot.value as? [[String : Optional <String>]]
{
post.append(itemArray)
self.myTableView.reloadData()
}
}
)}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
myIndex = indexPath.row
performSegue(withIdentifier: "detailsegue", sender: self)
}
@IBAction func quickAdd(_ sender: UIButton) // Link to add button on FirstViewController
{
uploadToFirebase(title: "Post the task?", message: "Enter a title and description for your task")
}
func uploadToFirebase (title: String, message: String)
{
let alert = UIAlertController(title: "Enter Details for Your Post", message: "", preferredStyle: UIAlertController.Style.alert)
alert.addTextField(configurationHandler: { (titleField : UITextField!) in // Adds a textfield to the alert
titleField.placeholder = "Enter item"
})
alert.addTextField(configurationHandler: { (descField : UITextField!) in // Adds a textfield to the alert
descField.placeholder = "Enter Description"
})
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.default, handler: { // Adds cancel button to alert; voids whatever has been inputted
(action : UIAlertAction!) -> Void in })
let saveAction = UIAlertAction(title: "Save", style: UIAlertAction.Style.default, handler: { (action : UIAlertAction! ) in // Adds save button to alert; saves the tasks to Firebase if clicked
let titleField = alert.textFields![0] as UITextField
let descField = alert.textFields![1] as UITextField
if (titleField.text != "") // Checks to make sure titleField isn't empty
{
if (descField.text != "") // Checks to make sure descField isn't empty
{
let key = ref?.child("task").childByAutoId().key
let post = ["uid": key, // Gets auto generated ID for database entry
"title": titleField.text, // Saves the title field to Firebase
"description": descField.text] // Saves the description field to Firebase
ref?.child("task").child(key!).setValue(post) // Saves the task for Firebase, ties each post with a unique ID
}
}
})
alert.addAction(saveAction)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil) // Presents the alert on screen
}
}
Here is my Firebase Database. The data under the child "task" doesn't appear on the UITableView
{
"task" : {
"-LT4Xtdo7Z0oyqHXHgif" : {
"description" : "4:02 desc",
"title" : "4:02 test",
"uid" : "-LT4Xtdo7Z0oyqHXHgif"
},
"-LU6eMyaKN9DZF-YYiLU" : {
"description" : "You Know!",
"title" : "HU",
"uid" : "-LU6eMyaKN9DZF-YYiLU"
},
swift
I had a working solution before, but I couldn't find a way to delete an entry on Firebase. The old solution only deleted entries on the local device(only the "list" and "desc" arrays would delete from the local device, but not the database). So I added the "post" variable to keep each title and description tied together with a unique ID, so I may retrieve that ID to delete the entry from the database.
Now using the "post" variable, the title and description uploads to Firebase but I'm having trouble retrieving and displaying data that I posted on firebase on my UITableView. I get an error when I try to append snapshot data into my dictionary named "post".
An error occurs at this line:
if let itemArray = snapshot.value as? [[String : Optional <String>]]
{
post.append(itemArray) \ERROR: Value of type '[String : Optional<String>]' has no member 'append'
self.myTableView.reloadData()
}
}
Here is the full code below
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
//******THIS IS THE TABLEVIEWCONTROLLER******
@IBOutlet weak var myTableView: UITableView! // link to tableview item in storyboard
//required function when using tableview; wants to know how many cells to produce (takes template from prototype cell in storyboard)
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return(list.count)
}
// required function when using tableview; actual link to each cell from storyboard
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = list[indexPath.row]
return (cell)
}
//function allows you to swipe left to delete an entry(cell)
public func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath){
if editingStyle == UITableViewCell.EditingStyle.delete
{
var key = [String]()
ref?.child(key[indexPath.row]).setValue(nil) // removes a entry on Firebase using its unique ID
key.remove(at: indexPath.row)
list.remove(at: indexPath.row)
desc.remove(at: indexPath.row)
myTableView.reloadData()
}
}
override func viewDidAppear(_ animated: Bool) {//change "viewDidLoad()" back to "viewDidAppear()"
ref = Database.database().reference() // sets the variable "ref" to connect to our Firebase database
list.removeAll() // Deletes all older data, so only data thats on the Firebase Database will be added to the "list" array
desc.removeAll() // Deletes all older data, so only data thats on the Firebase Database will be added to the "desc" array
let alert = UIAlertController(title: "Enter Details for Your Post", message: "", preferredStyle: UIAlertController.Style.alert)
let titleField = alert.textFields![0] as UITextField
let descField = alert.textFields![1] as UITextField
let key = ref?.child("task").childByAutoId().key
let post = ["uid": key, // Gets auto generated ID for database entry
"title": titleField.text, // Saves the title field to Firebase
"description": descField.text] // Saves the description field to Firebase
handle = ref?.child("task").observe(.childAdded, with: { (snapshot) in // looks for each title in the tasks'title directory in the Firebase database, so it may display them on the tableview
if let itemArray = snapshot.value as? [[String : Optional <String>]]
{
post.append(itemArray)
self.myTableView.reloadData()
}
}
)}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
myIndex = indexPath.row
performSegue(withIdentifier: "detailsegue", sender: self)
}
@IBAction func quickAdd(_ sender: UIButton) // Link to add button on FirstViewController
{
uploadToFirebase(title: "Post the task?", message: "Enter a title and description for your task")
}
func uploadToFirebase (title: String, message: String)
{
let alert = UIAlertController(title: "Enter Details for Your Post", message: "", preferredStyle: UIAlertController.Style.alert)
alert.addTextField(configurationHandler: { (titleField : UITextField!) in // Adds a textfield to the alert
titleField.placeholder = "Enter item"
})
alert.addTextField(configurationHandler: { (descField : UITextField!) in // Adds a textfield to the alert
descField.placeholder = "Enter Description"
})
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.default, handler: { // Adds cancel button to alert; voids whatever has been inputted
(action : UIAlertAction!) -> Void in })
let saveAction = UIAlertAction(title: "Save", style: UIAlertAction.Style.default, handler: { (action : UIAlertAction! ) in // Adds save button to alert; saves the tasks to Firebase if clicked
let titleField = alert.textFields![0] as UITextField
let descField = alert.textFields![1] as UITextField
if (titleField.text != "") // Checks to make sure titleField isn't empty
{
if (descField.text != "") // Checks to make sure descField isn't empty
{
let key = ref?.child("task").childByAutoId().key
let post = ["uid": key, // Gets auto generated ID for database entry
"title": titleField.text, // Saves the title field to Firebase
"description": descField.text] // Saves the description field to Firebase
ref?.child("task").child(key!).setValue(post) // Saves the task for Firebase, ties each post with a unique ID
}
}
})
alert.addAction(saveAction)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil) // Presents the alert on screen
}
}
Here is my Firebase Database. The data under the child "task" doesn't appear on the UITableView
{
"task" : {
"-LT4Xtdo7Z0oyqHXHgif" : {
"description" : "4:02 desc",
"title" : "4:02 test",
"uid" : "-LT4Xtdo7Z0oyqHXHgif"
},
"-LU6eMyaKN9DZF-YYiLU" : {
"description" : "You Know!",
"title" : "HU",
"uid" : "-LU6eMyaKN9DZF-YYiLU"
},
swift
swift
asked Dec 20 '18 at 21:19
heyylateefheyylateef
54
54
You are setting “post” as a dictionary. Dictionary items cannot be added with append. They need a [key,value] pair.
– Galo Torres Sevilla
Dec 20 '18 at 21:52
add a comment |
You are setting “post” as a dictionary. Dictionary items cannot be added with append. They need a [key,value] pair.
– Galo Torres Sevilla
Dec 20 '18 at 21:52
You are setting “post” as a dictionary. Dictionary items cannot be added with append. They need a [key,value] pair.
– Galo Torres Sevilla
Dec 20 '18 at 21:52
You are setting “post” as a dictionary. Dictionary items cannot be added with append. They need a [key,value] pair.
– Galo Torres Sevilla
Dec 20 '18 at 21:52
add a comment |
1 Answer
1
active
oldest
votes
Create your model File in below format
class ListModel: NSObject {
var UID:String?
var Title:String?
var Description:String?
}
In Your ViewController
var Obj : ListModel?
var ListArr = [ListModel]()
let ref = Database.database().reference().child("tasks")
ref.observe(.childAdded, with: { (snapshot) in
print(snapshot)
guard let dictionary = snapshot.value as? [String : AnyObject]
else {
return
}
let Obj = ListModel()
Obj.UID = snapshot.key
Obj.Title= dictionary["title"] as? String
Obj.Description = dictionary["description"] as? String
self.ListArr.append(Obj)
//ReloadTable Here
tableView.reloadData()
}, withCancel: nil)
I added exactly what you typed, I'm still getting a blank View Controller. The output log shows that the "post" is being uploaded, but it isn't displayed
– heyylateef
Dec 28 '18 at 5:22
use print(ListArr) to check if the data is added to ListArr. Also check dictionary if the data is there
– Yogesh Tandel
Dec 28 '18 at 5:23
Let me know if it worked.
– Yogesh Tandel
Dec 28 '18 at 5:56
I used print(ListArr) and print(dictionary) to verify that things are being added to those variables, and yes. I now just realized my my problem may be in the numberOfRowsInSection and cellForRowAt indexPath functions. Do I use ListArr.count for numberOfRowsInSection? What should I use for cellForRowAt indexPath?
– heyylateef
Dec 28 '18 at 6:04
yes use ListArr.count -- In cellforrow use-- cell.lbl_title.text = ListArr![indexPath.row].title cell.lbl_desc.text = ListArr![indexPath.row].description
– Yogesh Tandel
Dec 28 '18 at 6:31
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53876194%2fcant-view-dictionary-data-from-firebase%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Create your model File in below format
class ListModel: NSObject {
var UID:String?
var Title:String?
var Description:String?
}
In Your ViewController
var Obj : ListModel?
var ListArr = [ListModel]()
let ref = Database.database().reference().child("tasks")
ref.observe(.childAdded, with: { (snapshot) in
print(snapshot)
guard let dictionary = snapshot.value as? [String : AnyObject]
else {
return
}
let Obj = ListModel()
Obj.UID = snapshot.key
Obj.Title= dictionary["title"] as? String
Obj.Description = dictionary["description"] as? String
self.ListArr.append(Obj)
//ReloadTable Here
tableView.reloadData()
}, withCancel: nil)
I added exactly what you typed, I'm still getting a blank View Controller. The output log shows that the "post" is being uploaded, but it isn't displayed
– heyylateef
Dec 28 '18 at 5:22
use print(ListArr) to check if the data is added to ListArr. Also check dictionary if the data is there
– Yogesh Tandel
Dec 28 '18 at 5:23
Let me know if it worked.
– Yogesh Tandel
Dec 28 '18 at 5:56
I used print(ListArr) and print(dictionary) to verify that things are being added to those variables, and yes. I now just realized my my problem may be in the numberOfRowsInSection and cellForRowAt indexPath functions. Do I use ListArr.count for numberOfRowsInSection? What should I use for cellForRowAt indexPath?
– heyylateef
Dec 28 '18 at 6:04
yes use ListArr.count -- In cellforrow use-- cell.lbl_title.text = ListArr![indexPath.row].title cell.lbl_desc.text = ListArr![indexPath.row].description
– Yogesh Tandel
Dec 28 '18 at 6:31
|
show 1 more comment
Create your model File in below format
class ListModel: NSObject {
var UID:String?
var Title:String?
var Description:String?
}
In Your ViewController
var Obj : ListModel?
var ListArr = [ListModel]()
let ref = Database.database().reference().child("tasks")
ref.observe(.childAdded, with: { (snapshot) in
print(snapshot)
guard let dictionary = snapshot.value as? [String : AnyObject]
else {
return
}
let Obj = ListModel()
Obj.UID = snapshot.key
Obj.Title= dictionary["title"] as? String
Obj.Description = dictionary["description"] as? String
self.ListArr.append(Obj)
//ReloadTable Here
tableView.reloadData()
}, withCancel: nil)
I added exactly what you typed, I'm still getting a blank View Controller. The output log shows that the "post" is being uploaded, but it isn't displayed
– heyylateef
Dec 28 '18 at 5:22
use print(ListArr) to check if the data is added to ListArr. Also check dictionary if the data is there
– Yogesh Tandel
Dec 28 '18 at 5:23
Let me know if it worked.
– Yogesh Tandel
Dec 28 '18 at 5:56
I used print(ListArr) and print(dictionary) to verify that things are being added to those variables, and yes. I now just realized my my problem may be in the numberOfRowsInSection and cellForRowAt indexPath functions. Do I use ListArr.count for numberOfRowsInSection? What should I use for cellForRowAt indexPath?
– heyylateef
Dec 28 '18 at 6:04
yes use ListArr.count -- In cellforrow use-- cell.lbl_title.text = ListArr![indexPath.row].title cell.lbl_desc.text = ListArr![indexPath.row].description
– Yogesh Tandel
Dec 28 '18 at 6:31
|
show 1 more comment
Create your model File in below format
class ListModel: NSObject {
var UID:String?
var Title:String?
var Description:String?
}
In Your ViewController
var Obj : ListModel?
var ListArr = [ListModel]()
let ref = Database.database().reference().child("tasks")
ref.observe(.childAdded, with: { (snapshot) in
print(snapshot)
guard let dictionary = snapshot.value as? [String : AnyObject]
else {
return
}
let Obj = ListModel()
Obj.UID = snapshot.key
Obj.Title= dictionary["title"] as? String
Obj.Description = dictionary["description"] as? String
self.ListArr.append(Obj)
//ReloadTable Here
tableView.reloadData()
}, withCancel: nil)
Create your model File in below format
class ListModel: NSObject {
var UID:String?
var Title:String?
var Description:String?
}
In Your ViewController
var Obj : ListModel?
var ListArr = [ListModel]()
let ref = Database.database().reference().child("tasks")
ref.observe(.childAdded, with: { (snapshot) in
print(snapshot)
guard let dictionary = snapshot.value as? [String : AnyObject]
else {
return
}
let Obj = ListModel()
Obj.UID = snapshot.key
Obj.Title= dictionary["title"] as? String
Obj.Description = dictionary["description"] as? String
self.ListArr.append(Obj)
//ReloadTable Here
tableView.reloadData()
}, withCancel: nil)
edited Dec 28 '18 at 5:25
answered Dec 22 '18 at 12:11
Yogesh TandelYogesh Tandel
628711
628711
I added exactly what you typed, I'm still getting a blank View Controller. The output log shows that the "post" is being uploaded, but it isn't displayed
– heyylateef
Dec 28 '18 at 5:22
use print(ListArr) to check if the data is added to ListArr. Also check dictionary if the data is there
– Yogesh Tandel
Dec 28 '18 at 5:23
Let me know if it worked.
– Yogesh Tandel
Dec 28 '18 at 5:56
I used print(ListArr) and print(dictionary) to verify that things are being added to those variables, and yes. I now just realized my my problem may be in the numberOfRowsInSection and cellForRowAt indexPath functions. Do I use ListArr.count for numberOfRowsInSection? What should I use for cellForRowAt indexPath?
– heyylateef
Dec 28 '18 at 6:04
yes use ListArr.count -- In cellforrow use-- cell.lbl_title.text = ListArr![indexPath.row].title cell.lbl_desc.text = ListArr![indexPath.row].description
– Yogesh Tandel
Dec 28 '18 at 6:31
|
show 1 more comment
I added exactly what you typed, I'm still getting a blank View Controller. The output log shows that the "post" is being uploaded, but it isn't displayed
– heyylateef
Dec 28 '18 at 5:22
use print(ListArr) to check if the data is added to ListArr. Also check dictionary if the data is there
– Yogesh Tandel
Dec 28 '18 at 5:23
Let me know if it worked.
– Yogesh Tandel
Dec 28 '18 at 5:56
I used print(ListArr) and print(dictionary) to verify that things are being added to those variables, and yes. I now just realized my my problem may be in the numberOfRowsInSection and cellForRowAt indexPath functions. Do I use ListArr.count for numberOfRowsInSection? What should I use for cellForRowAt indexPath?
– heyylateef
Dec 28 '18 at 6:04
yes use ListArr.count -- In cellforrow use-- cell.lbl_title.text = ListArr![indexPath.row].title cell.lbl_desc.text = ListArr![indexPath.row].description
– Yogesh Tandel
Dec 28 '18 at 6:31
I added exactly what you typed, I'm still getting a blank View Controller. The output log shows that the "post" is being uploaded, but it isn't displayed
– heyylateef
Dec 28 '18 at 5:22
I added exactly what you typed, I'm still getting a blank View Controller. The output log shows that the "post" is being uploaded, but it isn't displayed
– heyylateef
Dec 28 '18 at 5:22
use print(ListArr) to check if the data is added to ListArr. Also check dictionary if the data is there
– Yogesh Tandel
Dec 28 '18 at 5:23
use print(ListArr) to check if the data is added to ListArr. Also check dictionary if the data is there
– Yogesh Tandel
Dec 28 '18 at 5:23
Let me know if it worked.
– Yogesh Tandel
Dec 28 '18 at 5:56
Let me know if it worked.
– Yogesh Tandel
Dec 28 '18 at 5:56
I used print(ListArr) and print(dictionary) to verify that things are being added to those variables, and yes. I now just realized my my problem may be in the numberOfRowsInSection and cellForRowAt indexPath functions. Do I use ListArr.count for numberOfRowsInSection? What should I use for cellForRowAt indexPath?
– heyylateef
Dec 28 '18 at 6:04
I used print(ListArr) and print(dictionary) to verify that things are being added to those variables, and yes. I now just realized my my problem may be in the numberOfRowsInSection and cellForRowAt indexPath functions. Do I use ListArr.count for numberOfRowsInSection? What should I use for cellForRowAt indexPath?
– heyylateef
Dec 28 '18 at 6:04
yes use ListArr.count -- In cellforrow use-- cell.lbl_title.text = ListArr![indexPath.row].title cell.lbl_desc.text = ListArr![indexPath.row].description
– Yogesh Tandel
Dec 28 '18 at 6:31
yes use ListArr.count -- In cellforrow use-- cell.lbl_title.text = ListArr![indexPath.row].title cell.lbl_desc.text = ListArr![indexPath.row].description
– Yogesh Tandel
Dec 28 '18 at 6:31
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53876194%2fcant-view-dictionary-data-from-firebase%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You are setting “post” as a dictionary. Dictionary items cannot be added with append. They need a [key,value] pair.
– Galo Torres Sevilla
Dec 20 '18 at 21:52