【问题标题】:How to save an Integer to Parse in Swift如何保存整数以在 Swift 中解析
【发布时间】:2015-08-14 04:45:02
【问题描述】:

我正在尝试将 integer 值保存到 Parse.com,但出现错误。我将 Parse 类中的对象设置为 Number

这是我的代码:

  if let currentUser = PFUser.currentUser() {

        currentUser.fetchIfNeededInBackgroundWithBlock({ (foundUser: PFObject?, error: NSError?) -> Void in

            // Get and update score

            if foundUser != nil {

                let score = foundUser!["score"] as! Int

                let points = 100 + score

                foundUser!["score"] = points

                foundUser?.saveInBackgroundWithBlock({ (succeeded: Bool, error: NSError?) -> Void in

                    if succeeded {

                        println("score added to user")
                    }
                })

            }

        })

    }

有人可以帮忙吗?

谢谢

【问题讨论】:

  • 你的错误是什么,你在哪一行得到它?
  • 让 score = foundUser!["score"] as!诠释
  • 我得到这个错误:致命错误:在展开可选值时意外发现 nil
  • 打印您的 score 变量并检查是否可以将其转换为 int。
  • 分数打印描述:6171707936 分数打印描述:6176593344

标签: swift parse-platform integer


【解决方案1】:

发生此错误是因为您将 nil 转换为 Int。

我认为它有效:

if let currentUser = PFUser.currentUser() {

    currentUser.fetchIfNeededInBackgroundWithBlock({ (foundUser: PFObject?, error: NSError?) -> Void in

        // Get and update score

        if let foundUser = foundUser {
            if let score = foundUser["score"] as? Int {
                foundUser["score"] = 100 + score
            } else {
                foundUser["score"] = 0
            }

            foundUser.saveInBackgroundWithBlock({ (succeeded: Bool, error: NSError?) -> Void in

                if succeeded {

                    println("score added to user")
                }
            })

        }
    })
}

【讨论】:

  • 它让我改变 if let foundUser = foundUser, score = foundUser["score"] 到 if let foundUser = foundUser, score: AnyObject = foundUser["score"] and foundUser["score"] = 积分。由于某种原因它也没有保存到服务器,但它仍然运行,所以我想这是一个开始。
  • 呃,由于某种原因,新分数没有保存“添加给用户的分数”甚至没有打印出来
  • 可以这样打印错误:if succeeded { println("score added to user") } else { println(error) }
  • 天哪,终于成功了!!!!!!非常感谢你的帮助!!!你不知道我已经尝试解决这个问题多久了!!!!!!!!!
【解决方案2】:

我认为您应该通过这种方式将您的 foundUser!["score"] 转换为 Int

let score = foundUser?["score"].integerValue

编辑

if let currentUser = PFUser.currentUser() {

        currentUser.fetchIfNeededInBackgroundWithBlock({ (foundUser: PFObject?, error: NSError?) -> Void in

            // Get and update score

            if foundUser != nil {

                if let score = foundUser?["score"].integerValue {
                    let points = 100 + score

                    foundUser!["score"] = points

                    foundUser?.saveInBackgroundWithBlock({ (succeeded: Bool, error: NSError?) -> Void in

                        if succeeded {

                            println("score added to user")
                        }
                    })
                }
            }

        })

    }

希望它会有所帮助。

【讨论】:

  • 呃,它仍然不起作用..哈哈,我确实已经在这个问题上工作了 5 个小时。我不明白为什么这么复杂。你会认为这很简单。就像我在上面所说的那样,我认为我的问题在于“if foundUser!= nil”,因为它仅在列中已经存在值时才运行。你知道在没有 foundUser !=nil 的情况下执行我的代码的方法吗?
  • 您可以使用if let 并打开它。
  • 由于某种原因它仍然没有保存。我将尝试将 Parse 中的分数列更改为字符串
  • 如果用户不存在,您是否尝试保存该用户?
  • @Bannings 不,我正在尝试将分数保存给当前用户
猜你喜欢
  • 1970-01-01
  • 2018-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多