arrays - 使用 Swift 追加数组时出现意外错误

标签 arrays swift xcode6

    var playerOneArray : Array<(Int,Int,Int)> = []
    var currentPerson = 1
    var currentWeapon = 1
    var currentRoom = 1
    var currentPlayer = 1

    currentPerson = everyPicker.selectedRowInComponent(0)
    currentWeapon = everyPicker.selectedRowInComponent(1)
    currentRoom = everyPicker.selectedRowInComponent(2)
    currentPlayer = playerPicker.selectedRowInComponent(0)

    //In an if statement
    playerOneArray.append(currentRoom, currentPerson, currentWeapon) as (Int,Int,Int)
    // Error tuple types () and (Int,Int,Int) have a different number of elements (0 vs. 3)

即使我去掉 as int,int,int 仍然有错误,我不知道为什么会这样。如果我把它拿走,出现的错误是访问协议(protocol)“int”的成员未实现。

最佳答案

您没有关闭 append 调用的括号。

但是,因为 swift 知道 playerOneArray 是一个包含 3 个 Int 的数组。您可以简单地将 3 个变量传递给 append 方法,如下所示:

 playerOneArray.append(currentRoom, currentPerson, currentWeapon) 

假设 (currentRoom, currentPerson, currentWeapon) 是 Int 值的元组。这会将 (currentRoom, currentPerson, currentWeapon) 存储到 playerOneArray[0] 中。

作为旁注,您似乎想要一组包含每个玩家详细信息的玩家。如果是这种情况,您应该将 playerOneArray 重命名为 players 并简单地添加每个玩家的信息。这样每个索引将代表玩家信息(整数元组)。

关于arrays - 使用 Swift 追加数组时出现意外错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29526577/

相关文章:

C# 将 JSON 对象转换为数组

python - 格式化为整数的 numpy savetxt 不保存零

json - alamofire 编码完成 JSON 到字典

jquery - 无法将对象作为参数传递

json - Swift - 在 Post 请求上向 httpBody 添加参数

ios - 我想设计一个屏幕,根据特定条件在同一屏幕上显示不同数量的视频

ios - XCode 找不到错误的 .app 文件

ios - 框架中的 Swift @IBInspectable 属性未显示给 Interface Builder

macos - 如何控制另一个 View Controller 中的实例

javascript - 将对象添加到数组中,然后显示在表格中