collectionview单元内的按钮操作-尝试使用新的未保存的子级保存对象-

时间:2018-12-10 12:12:16

标签: ios swift parsing uicollectionview uibutton

我在collectionview单元格内有一个uibutton。用户可以使用此按钮喜欢他们的帖子。但是我遇到了以下错误:“线程1:致命错误:在展开可选值时意外发现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] 给出了错误

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

可选类型'PFObject?'的值必须解包为'PFObject'类型的值和线程1:EXC_BAD_INSTRUCTION(code = EXC_I386_INVOP,subcode = 0x0)

0 个答案:

没有答案