首页 > 解决方案 > 无法将“__NSCFNumber”(0x10ec0ba38)类型的值转换为“NSDictionary”(0x10ec0ca78)

问题描述

我有一个带有帖子的表格视图,我正在尝试创建相当于喜欢帖子的按钮。我专注于让其中一个按钮工作,然后我将镜像下一个按钮的代码。每次我点击它都会使模拟器崩溃并抛出错误

“无法将 '__NSCFNumber' (0x10ec0ba38) 类型的值转换为 'NSDictionary' (0x10ec0ca78)。”

我使用 firebase 作为我的后端。我真的是 swift 新手,任何帮助都将不胜感激。

***这是我的表格视图单元格***

class PrayerWallTableViewCell: UITableViewCell {

    @IBOutlet weak var userNameLabel: UILabel!
    @IBOutlet weak var prayerRequestLabel: UILabel!
    @IBOutlet weak var wallTimeStamp: UILabel!
    @IBOutlet weak var prayingButton: UIButton!
    @IBOutlet weak var likeButtonCount: UILabel!
    @IBOutlet weak var prayerButtonCount: UILabel!

    var prayRef:DatabaseReference!

    @IBAction func likeButton_TouchUpInside(_ sender: Any) {


        prayRef = Database.database().reference().child("Prayers")
         incrementLikes(forRef: prayRef)
    }

    func incrementLikes(forRef ref:DatabaseReference){
        ref.runTransactionBlock({ (currentData: MutableData) -> TransactionResult in
            if var post = currentData.value as? [String : AnyObject], let uid = Auth.auth().currentUser?.uid {
                var stars: Dictionary<String, Bool>
                stars = post["stars"] as? [String : Bool] ?? [:]
                var starCount = post["starCount"] as? Int ?? 0
                if let _ = stars[uid] {
                    // Unstar the post and remove self from stars
                    starCount -= 1
                    stars.removeValue(forKey: uid)
                } else {
                    // Star the post and add self to stars
                    starCount += 1
                    stars[uid] = true
                }
                post["starCount"] = starCount as AnyObject?
                post["stars"] = stars as AnyObject?

                // Set value and report transaction success
                currentData.value = post

                return TransactionResult.success(withValue: currentData)
            }
            return TransactionResult.success(withValue: currentData)
        }) { (error, committed, snapshot) in
            if let error = error {
                print(error.localizedDescription)
            }
        }
    }

****这是我的带有tableview协议的viewcontroller*****

 var prayerRef: DatabaseReference?
    var prayerDatabaseHandle:DatabaseHandle?

    var prayerRequest = [String]()
    var prayerTimestamps:[NSDate] = []
    var userName = [String]()

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        prayerRef = Database.database().reference()

        tableView.reloadData()

        tableView.transform = CGAffineTransform(rotationAngle: -CGFloat.pi)

        tableView.delegate = self
        tableView.dataSource = self

        prayerDatabaseHandle = prayerRef?.child("Prayers").observe(.childAdded, with: { (snaphot) in

            let prayPost = snaphot.value as! [String: Any]

            let praytimestamp = prayPost["praydate"] as! TimeInterval
            let prayTimeDate:NSDate = NSDate(timeIntervalSince1970: praytimestamp/1000)

            self.prayerTimestamps.append(prayTimeDate)

            //self.prayerRequest.append(pray
          //  [""] as! String)                

            self.userName.append(prayPost["username"] as! String)                

        self.prayerRequest.append(prayPost["prayer"] as! String)           

      self.tableView.reloadData()

    })
}
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return prayerRequest.count
    }

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

    cell.prayerRequestLabel.lineBreakMode = .byWordWrapping
    cell.prayerRequestLabel.numberOfLines = 8

    cell.prayerRequestLabel.text = prayerRequest[indexPath.row]

    cell.userNameLabel.text! = userName[indexPath.row]

        let prayTempTimestamp:NSDate = prayerTimestamps[indexPath.row]
        let prayTimestampDate:Date = prayTempTimestamp as Date            

        cell.wallTimeStamp.text = prayTimestampDate.timeAgoDisplay()

        cell.transform = CGAffineTransform(rotationAngle: CGFloat.pi)

        return cell            
    }
}

当我单击按钮并且模拟器崩溃时,这是我带来的

标签: swift

解决方案


您的代码中有一个标签或变量返回一个 Int 值,您将其视为 NSDictionary。首先在邮递员中检查您的回复,然后发布它的屏幕截图,以便我提供更多帮助。


推荐阅读