ios Mapfirebase文档的问题:传递给调用的参数不带参数

py49o6xq  于 2023-05-02  发布在  iOS
关注(0)|答案(1)|浏览(108)

我对Xcode 14有一个有趣的问题。3,firebase 9.6.0.
我的代码是从firebase获取文档,我想将收到的文档Map到一个自定义结构“游戏”

struct Game: Identifiable, Codable {  
    @DocumentID var id: String?             // -> UUID of firestore document
    var nr: Int?
    var status: String?
    var league: String
    var homeTeam: String
    var homeTeamLogoUrl: String?
    var guestTeam: String
    var guestTeamLogoUrl: String?
    var runs: Runs?
    var scheduledDate: Date
    var startTime: Date?
    var endTime: Date?
    var ballpark: String
    var city: String
    var scorerID: String?
    var score: Count?
    var bases: BaseStatus?
    var inning: Int?
    var inningHalf: String?
    var inningResults: [InningResult]?
}

func getLiveGames(country: String, league: String, season: Int) async {
    // declare temporary list of live games
    var liveGames = [Game]()

    let leagueRef = db.collection("liveGames")
    
    let snapshot = try? await leagueRef.getDocuments()

    if snapshot != nil {
        for docs in snapshot!.documents {
            do {
                let result = try docs.data(as: Game.self)
                print(result)
            } catch {
                print(error)
            }  
        }
    } else {
        self.errorMessage = "No documents in 'liveGames' collection"
        print(self.errorMessage)
    }
}

甚至我也遵循了Firebase帮助中的描述:https://firebase.google.com/docs/firestore/solutions/swift-codable-data-mapping?hl=en
Xcode显示以下错误消息:传递给call的参数不带参数。
enter image description here
即使Xcode显示这个错误消息,它也会编译并运行我的代码。
有人经历过同样的行为吗?
其中非常有趣的部分是,使用一点不同的代码,Xcode不会编译,也不会运行代码。但这才是我真正想要的编码方式。..

func getLiveGames(country: String, league: String, season: Int) async {
    // declare temporary list of scheduled games
    var liveGames = [Game]()

    let leagueRef = db.collection("liveGames")
    
    let queryLiveGames = try? await leagueRef.getDocuments()
     
    if queryLiveGames != nil {
        liveGames = (queryLiveGames!.documents.compactMap({ (queryDocumentSnapshot) -> Game? in
        let result = Result { try? queryDocumentSnapshot.data(as: Game.self) }

        switch result {
        case .success(let game):
             if var game = game {
                 // A game value was successfully initialized from the DocumentSnapshot
                 self?.errorMessage = nil
                 liveGames.append(game)
                 return game
             }
             else {
                 // A nil value was initialized from the DocumentSnapshot,
                 // or the DocumentSnapshot was nil
                 self.errorMessage = "Game document doesn't exist."
                 return nil
             }
         
        case .failure(let error):
        // A liveGame value could not be initialized from the DocumentSnapshot
             switch error {
                 case DecodingError.typeMismatch(_, let context):
                 self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
                 case DecodingError.valueNotFound(_, let context):
                 self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
                 case DecodingError.keyNotFound(_, let context):
                 self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
                 case DecodingError.dataCorrupted(let key):
                 self.errorMessage = "\(error.localizedDescription): \(key)"
                 default:
                 self.errorMessage = "Error decoding document: \(error.localizedDescription)"
             }
             return nil
        }
        }))
    } else {
        self.errorMessage = "No documents in 'liveGames' collection"
        print(self.errorMessage)
    }
}

Xcode-截图:

3pvhb19x

3pvhb19x1#

添加FirebaseFirestoreSwift作为Xcode目标和import FirebaseFirestoreSwift的依赖项。

相关问题