ParseSwift 未将 Int 保存到列 - 未定义

问题描述 投票:0回答:1

我有一个文档中定义的简单 GameScore 对象(https://github.com/parse-community/Parse-Swift/blob/main/ParseSwift.playground/Pages/1%20-%20Your%20first%20Object。 xcplaygroundpage/Contents.swift)

import Foundation
import ParseSwift

struct GameScore: ParseObject {
    var objectId: String?
    var createdAt: Date?
    var updatedAt: Date?
    var ACL: ParseACL?
    var originalData: Data?


    // Your own properties
    var score: Int?
    var name: String?

    func merge(with object: Self) throws -> Self {
        var updated = try mergeParse(with: object)
        if updated.shouldRestoreKey(\.score, original: object) {
            updated.score = object.score
        }
        if updated.shouldRestoreKey(\.name, original: object) {
            updated.name = object.name
        }
        return updated
    }
}
extension GameScore {

    init(score: Int) {
        self.score = score
    }

    init(score: Int, name: String) {
        self.score = score
        self.name = name
    }

    init(objectId: String?) {
        self.objectId = objectId
    }
}

然后我通过调用此函数将 GameScore 对象保存到服务器:

func createScore() {
    var score = GameScore(score: Int(scoreScore), name: scoreName)
    score.score = Int(scoreScore)
    score.save { [weak self] result in
        switch result {
        case .success(let savedScore):
            score = savedScore
            self?.gameScore = savedScore
            self?.fetchGameScores()
            print("savedScore - score: \(score.score ?? 0), name: \(score.name ?? "")")
        case .failure(let error):
            print("save error: \(error.localizedDescription)")
        }
    }
}

现在,当我将此对象保存到 Parse 服务器时,它会保存它,但

score
属性始终未定义。我究竟做错了什么?我还尝试将
score
属性和increment() 操作增加1,例如按预期工作,分数变为1。但它从不接受我设置分数的初始值。

这应该非常简单易行,但它不起作用,我不明白为什么。

注意:我使用的是 ParseSwift 库,而不是 Obj-C 中的标准 Parse iOS SDK

更新

当我在保存函数回调块中放置断点时,我确实看到 SaveScore 在:

case .success(let savedScore)
确实有 100 分或未定义的分数。那么问题可能是我的 Parse 服务器没有在仪表板中正确显示此 Int 值?这很奇怪。但是,它确实正确显示了 GameScore 对象上的
name
属性。此外,从服务器获取 GameScore 后,最新创建的 GameScore 看起来有一个未定义的分数,而不是响应回调所说的分数。所以服务器由于某种原因没有保存 Int 值。

更新2

我在 Parse 服务器中创建了另一列,并在 GameScore 对象中创建了一个名为

point
的相应属性(就像示例所示)只是为了测试它。使用
point
,它保存了我正确给出的初始值!我不知道
score
出了什么问题,但也许是关键字或其他不起作用的东西。

ios swift swiftui parse-platform parse-ios-sdk
1个回答
0
投票

score
是 Parse 中的受保护字段,并且该密钥被 skipped 因此它永远不会编码到服务器。

score
字段用于查询,更多查看这里

/**
 Conform to this protocol to add the required properties to your `ParseObject`
 for using `QueryConstraint.matchesText()` and `Query.sortByTextScore()`.
 - note: In order to sort you must use `Query.sortByTextScore()`.
   To retrieve the weight/rank, access the "score" property of your `ParseObject`.
 */

public protocol ParseQueryScorable {
    /**
     The weight/rank of a `QueryConstraint.matchesText()`.
    */
    var score: Double? { get }
}
© www.soinside.com 2019 - 2024. All rights reserved.