【问题标题】:SWIFT: Parse column is not appending into arraySWIFT:解析列未附加到数组中
【发布时间】:2015-11-04 17:03:16
【问题描述】:

我想使用 Swift 将以下解析表中的“userVotes”列附加到一个数组中 -

这是我的代码 -

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) {



}


}

我希望解析数组列附加如下 - [["user1,"user5,"user9"],["user1,"user2,"user3"],["user4,"user5,"user6"],...]

此时,我收到以下运行时错误 - 致命错误:在展开可选值时意外发现 nil

【问题讨论】:

    标签: ios swift parse-platform


    【解决方案1】:

    因为您的“userVotes”中的每个对象都是一个数组,并且您已经声明了

    var voters = [String]()
    

    这是不对的,因为您说将附加一个元素,但事实并非如此。

    因此,您应该将选民声明为...

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

    那么当你下载它时,

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-14
      • 2018-05-16
      • 1970-01-01
      • 2016-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多