集合视图单元格内的按钮操作 - 尝试使用未保存的新子节点保存对象 -

loading...


1

我在collectionview单元格中有一个uibutton . 用户可以通过此按钮喜欢他们的帖子 . 但是我得到了这些错误:'线程1:致命错误:在解开一个Optional值时意外发现nil'和'试图用一个未保存的新孩子保存一个对象'

对于集合视图按钮操作,我这样做了

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AdCell", for: indexPath) as! AdCell

    cell.likeOutlet.tag = indexPath.row
    cell.likeOutlet.addTarget(self,
                              action: #selector(self.likeButt),
                              for: .touchUpInside)

这是主视图控制器中的IBAction

@IBAction func likeButt(_ sender: UIButton) {
    if PFUser.current() != nil {

        let currUser = PFUser.current()!

        let query = PFQuery(className: LIKES_CLASS_NAME)
        query.whereKey(LIKES_CURR_USER, equalTo: currUser)
        query.whereKey(LIKES_AD_LIKED, equalTo: adObj)
        query.findObjectsInBackground { (objects, error) in
            if error == nil {

                if objects!.count == 0 
                {

                    let likeObj = PFObject(className: LIKES_CLASS_NAME)

                    likeObj[LIKES_CURR_USER] = currUser
                    likeObj[LIKES_AD_LIKED] = self.adObj
                    likeObj.saveInBackground(block: { (succ, error) in
                        if error == nil {
                            AdCell.appearance().likeOutlet.setImage(#imageLiteral(resourceName: "ad_liked"), for: .normal)
                            self.hideHUD()


                            self.adObj.incrementKey(ADS_LIKES, byAmount: 1)


                            if self.adObj[ADS_LIKED_BY] != nil {
                                var likedByArr = self.adObj[ADS_LIKED_BY] as! [String]
                                likedByArr.append(currUser.objectId!)
                                self.adObj[ADS_LIKED_BY] = likedByArr
                            } else {
                                var likedByArr = [String]()
                                likedByArr.append(currUser.objectId!)
                                self.adObj[ADS_LIKED_BY] = likedByArr
                            }
                            self.adObj.saveInBackground()


                            let sellerPointer = self.adObj[ADS_SELLER_POINTER] as! PFUser
                            let pushStr = "\(PFUser.current()![USER_USERNAME]!) liked your ad: \(self.adObj[ADS_TITLE]!)"

                            let data = [ "badge" : "Increment",
                                         "alert" : pushStr,
                                         "sound" : "bingbong.aiff"
                            ]
                            let request = [
                                "someKey" : sellerPointer.objectId!,
                                "data" : data
                                ] as [String : Any]

                            PFCloud.callFunction(inBackground: "push", withParameters: request as [String : Any], block: { (results, error) in
                                if error == nil {
                                    print ("\nPUSH SENT TO: \(sellerPointer[USER_USERNAME]!)\nMESSAGE: \(pushStr)\n")
                                } else {
                                    print ("\(error!.localizedDescription)")
                                }
                            })



                            // Save Activity
                            let activityClass = PFObject(className: ACTIVITY_CLASS_NAME)
                            activityClass[ACTIVITY_CURRENT_USER] = sellerPointer
                            activityClass[ACTIVITY_OTHER_USER] = PFUser.current()!
                            activityClass[ACTIVITY_TEXT] = pushStr
                            activityClass.saveInBackground()


                            // error on saving like
                        } else {
                            self.simpleAlert("\(error!.localizedDescription)")
                            self.hideHUD()
                        }})


                    // 3. UNLIKE THIS AD :(
                } else {
                    var likeObj = PFObject(className: LIKES_CLASS_NAME)
                    likeObj = objects![0]
                    likeObj.deleteInBackground(block: { (succ, error) in
                        if error == nil {
                            AdCell.appearance().likeOutlet.setImage(#imageLiteral(resourceName: "ad_no_like"), for: .normal)
                            self.hideHUD()


                            self.adObj.incrementKey(ADS_LIKES, byAmount: -1)


                            var likedByArr = self.adObj[ADS_LIKED_BY] as! [String]
                            likedByArr = likedByArr.filter { $0 != currUser.objectId! }
                            self.adObj[ADS_LIKED_BY] = likedByArr

                            self.adObj.saveInBackground()


                            //                                let likesNr = self.adObj[ADS_LIKES] as! Int
                            //                            self.likeLabel.text = "\(likesNr)"

                        } else {
                            self.simpleAlert("\(error!.localizedDescription)")
                            self.hideHUD()
                        }})
                }


                // error in query
            } else {
                self.simpleAlert("\(error!.localizedDescription)")
                self.hideHUD()
            }}

    } else {
        showLoginAlert("You need to login")
    }
}

在另一个页面中,此功能运行良好 . 但这里给出错误 . 我错过了什么?

编辑:

首先这一行给出了错误 if objects!.count == 0

我用 if objects?.count == 0 替换了这一行并且工作了

但现在 likeObj = objects![0] 给出了错误

当我再次写 objects?[0] 而不是 objects![0] 时,我遇到了这些错误

Value of optional type 'PFObject?' must be unwrapped to a value of type 'PFObject'Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

loading...

0回答

No Data

评论

暂时没有评论!