由于'Swift._NSContiguousString'到'PFObject'而终止应用

时间:2017-02-10 05:02:30

标签: uitableview parse-platform swift3

我对这个问题感到困惑,我为这个问题尝试了不同的'修复',但似乎没有任何效果。参考代码: class FindAParty:UITableViewController {

    var partyData:NSMutableArray! = NSMutableArray()
        //var user:NSMutableArray = NSMutableArray()

        override init(style: UITableViewStyle){
            super.init(style: style)

        }

        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            //fatalError("init(coder:) has not been implemented")

        }


        @IBAction func loadData(){
            print ("Load Data went through")
            partyData.removeAllObjects()
            print ("Remove ALL Objeccts")
            let findPartyData:PFQuery = PFQuery(className: "Party")
            print("PFQuery...")
            findPartyData.findObjectsInBackground{
                (objects:[PFObject]?, error:Error?)->Void in

                if error != nil {

                    print("Error")


                }else{

                    for object in objects!{

                        let party:PFObject = object as PFObject
                        self.partyData.add("party")
                        }

                    let array:NSArray = self.partyData.reverseObjectEnumerator().allObjects as NSArray
                    self.partyData = NSMutableArray(array: array)

                    self.tableView.reloadData()

                }
            }
        }

        override func viewDidAppear(_ animated: Bool) {
            self.loadData()
            print("View Did Appear")
        }
        override func viewDidLoad() {
            super.viewDidLoad()
            print("ViewDidLoad")
            //self.loadData()
            // Uncomment the following line to preserve selection between presentations
            // self.clearsSelectionOnViewWillAppear = false

            // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
            // self.navigationItem.rightBarButtonItem = self.editButtonItem
        }

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

        // #pragma mark - Table view data source
        override func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return partyData.count
        }


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

//Error Happens Below
            let party:PFObject = self.partyData.object(at: indexPath.row) as! PFObject





            cell.typeOfPartyLabel.alpha = 0
            cell.timeOfPartyLabel.alpha = 0
            cell.usernameLabel.alpha = 0

            cell.typeOfPartyLabel.text = party.object(forKey: "partyTitle") as? String
            cell.timeOfPartyLabel.text = party.object(forKey: "partyTime") as? String
            cell.usernameLabel.text = party.object(forKey: "Username") as? String

           // var dataFormatter:NSDateFormatter = NSDateFormatter()
            //dataFormatter.dateFormat = "yyyy-MM-dd HH:mm"
            //cell.timestampLabel.text = dataFormatter.stringFromDate(sweet.createdAt)

            let findUser:PFQuery = PFUser.query()!
            findUser.whereKey("objectId", equalTo: party.object(forKey: "Username")!)



            findUser.findObjectsInBackground {
                (objects:[PFObject]?, error: Error?) -> Void in // Changes NSError to Error
                if error == nil{

                    let user:PFUser = (objects)!.last as! PFUser
                    cell.usernameLabel.text = user.username

                    UIView.animate(withDuration: 0.5, animations: {
                        cell.typeOfPartyLabel.alpha = 1
                        cell.timeOfPartyLabel.alpha = 1
                        cell.usernameLabel.alpha = 1
                    })
                }
            }


            return cell
        }}

有人建议更改:let party:PFObject = self.partyData.object(at: indexPath.row) as! PFObject 致:

let party = self.partyData.object(at: indexPath!.row)

然而,这并没有解决它,我不知道我应该如何修复它成功的应用程序构建和它的其他方面运行,但这是唯一失败的部分。我正在使用Parse和亚马逊作为我的后端。

1 个答案:

答案 0 :(得分:1)

声明数据源数组

var partyData = [PFObject]()

并将loadData()更改为

    @IBAction func loadData(){
        print ("Load Data went through")
        partyData.removeAll()
        print ("Remove ALL Objeccts")
        let findPartyData = PFQuery(className: "Party")
        print("PFQuery...")
        findPartyData.findObjectsInBackground {
            (objects:[PFObject]?, error:Error?)->Void in

            if error != nil {
                print(error!)
            } else{
                if let objects = objects {
                    self.partyData = objects.reversed()
                }
                DispatchQueue.main.async {
                   self.tableView.reloadData()
                }
            }
        }
    }

并获得一行:

let party = self.partyData[indexPath.row]

最后在cellForRowAt

中替换
if error == nil {
   let user:PFUser = (objects)!.last as! PFUser

if users = objects as? [PFUser], !users.isEmpty {
   let user = users.last!