1

I would like two display Object data gotten from Parse in swift. I have tried using label in this way but it only displays the last element in the object. Please how can I make it display all the element in the object in the label. Like one element to one label. Thanks

 let query = PFQuery(className: "Questionnaire")
 query.findObjectsInBackground { (objects, error) -> Void in
    if error == nil {

     // There were no errors in the fetch
        if let returnedObjects = objects {
     // var text = ""
     // Objects Array is not nil
     // loop through the array to get each object

             for object in returnedObjects {
                 print(object["question"] as! String)
                 // text.append(object["question"] as! String)
                self.Label.text = (object["question"] as! String)


             }


        }
    }
 }
1
  • you can use tableview for display multiple labels Commented May 11, 2018 at 6:46

4 Answers 4

2

You can do in one line like that and join all question with , separator , you can change separator to any (empty, -,...etc)

if let returnedObjects = returnedObjects {
    self.Label.text = returnedObjects.map {($0["question"] as? String) ?? nil}.compactMap({$0}).joined(separator: ",")
}
Sign up to request clarification or add additional context in comments.

Comments

0

Use tableview for this.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! YouTableViewCell 
        cell.textLabel.text = yourArray[indexpath.row] as? String ?? ""          
        return cell

}

Comments

0

If it's important to use UILabel

var concatenatedString = ""

for object in returnedObjects {

    concatenatedString += object["question"] as! String
}

self.Label.text = concatenatedString

Comments

0

You are looping through the array and setting each value to Label.text. However, setting Label.text will replace what was on the label before. That's why you only see the last item.

One solution is to display the string representation of the array:

self.Label.text = "\(object)"

Another solution is to display the items in a table view Suganya Marlin has suggested. You would need to conform to UITableViewDatasource and implement the various methods. Here is a guide.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.