【发布时间】:2022-01-13 14:44:48
【问题描述】:
我的 firebase 文档中的一个字段是一个包含用户 ID 的字符串数组。
当我尝试在我的一个视图中初始化它时,我得到了错误
Cannot convert value of type 'String?' to expected element type 'Array<String>.ArrayLiteralElement' (aka 'String')
这就是我正在做的事情
@State var playerIds: [String] = [Auth.auth().currentUser?.uid, "SomeRandomHardcodedUserID"]
下面这行有效,但上面提到的那一行无效。
@State var playerIds: [String] = ["SomeHardcodedUID", "SomeOtherHardcodedUserID"]
我的意图是使用playerIds 构造一个可编码的对象以发送到 Firestore。
下面的行适用于硬编码的 userId。
let newGame = Game(gameTime: gameTime, playerIds: playerIds, inProgress: gameInProgress, winnerId: winnerId)
将当前用户的 userId 放入我的数组中的正确方法是什么?
【问题讨论】:
-
Auth.auth().currentUser?.uid:这是一个String?,即可选,所以它可以是nil。但是您将playerIds声明为[String],因此它不能包含可选值。 -
确实如此。我刚刚想通了,我也发布了一个答案。谢谢!
标签: swift google-cloud-firestore swiftui firebase-authentication