0

I'd like to append the 'userVotes' column in the following parse table into an array using Swift -

enter image description here

Here is my code -

import UIKit
import Parse

class MusicPlaylistTableViewController: UITableViewController {

var usernames = [String]()
var songs = [String]()
var voters = [String]()

var numVotes = 0

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.separatorColor = UIColor.grayColor()

    let query = PFQuery(className:"PlaylistData")
    query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) -> Void in

        if error == nil {

            if let objects = objects! as? [PFObject] {

                self.usernames.removeAll()
                self.songs.removeAll()
                self.voters.removeAll()

                for object in objects {

                    let username = object["username"] as? String
                    self.usernames.append(username!)

                    let track = object["song"] as? String
                    self.songs.append(track!)

                    let title = object["userVotes"]! as? String
                    self.voters.append(title!)
                    print("Array: \(self.voters)")

                }

                self.tableView.reloadData()
            }

        } else {

            print(error)
        }
    }


}

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

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1

}

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

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("CellTrack", forIndexPath: indexPath) as! TrackTableViewCell

    //cell.username.text = usernames[indexPath.row]
    cell.username.text = usernames[indexPath.row]
    cell.songTitle.text = songs[indexPath.row]
    cell.votes.text = "\(numVotes)"

    cell.selectionStyle = UITableViewCellSelectionStyle.None
    return cell
}

override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {



}


}

I would like the parse array column to append as follows - [["user1,"user5,"user9"],["user1,"user2,"user3"],["user4,"user5,"user6"],...]

At this point, I'm getting the following runtime error - fatal error: unexpectedly found nil while unwrapping an Optional value

1 Answer 1

2

Since each object that is in your "userVotes" is an array and your you've declared

var voters = [String]()

which is not right because you're saying that there will be one element being appended which is not the case.

So, you should declare voters as...

var voters = Array<Array<String>>()

then as you are downloading it,

for object in objects {
    let title = object["userVotes"]! as? [String]
    self.voters.append(title!)
    print("Array: \(self.voters)")
}
Sign up to request clarification or add additional context in comments.

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.