如何调用Swift Array.indexOf?

时间:2015-07-24 18:57:22

标签: arrays swift

这是一段代码,我得到一个我不明白的语法错误:

Private Sub DG_Paint(sender As Object, e As PaintEventArgs) Handles DG.Paint, DataGridView2.Paint, DataGridView3.Paint
    Dim DG As DataGridView = DirectCast(sender, DataGridView)

    Dim intRowHeight As Integer
    Dim intTopOffset As Integer
    Dim intLeftOffset As Integer
    Dim objRowImage As System.Drawing.Bitmap
    Dim objGraphics As System.Drawing.Graphics
    Dim objRowFrame As System.Drawing.Rectangle
    Dim objRowFill As System.Drawing.Rectangle
    Dim objRowHeaderFrame As System.Drawing.Rectangle
    Dim intIndex As Integer
    Dim intLastIndex As Integer
    Dim intRowHeadersWidth As Integer
    If DG.AllowUserToAddRows Then
        intLastIndex = DG.NewRowIndex
    Else
        intLastIndex = DG.Rows.Count - 1
    End If
    If DG.RowHeadersVisible Then
        intRowHeadersWidth = DG.RowHeadersWidth
    Else
        intRowHeadersWidth = 1
    End If
    intRowHeight = DG.RowTemplate.Height
    intTopOffset = DG.ColumnHeadersHeight + intRowHeight * (intLastIndex + 1)
    If DG.Height - intTopOffset > intRowHeight Then
        objRowImage = New System.Drawing.Bitmap(DG.Width, intRowHeight)
        objGraphics = System.Drawing.Graphics.FromImage(objRowImage)
        objRowFrame = New System.Drawing.Rectangle(0, 0, DG.Width, intRowHeight)
        objGraphics.DrawRectangle(System.Drawing.SystemPens.ControlDark, objRowFrame)
        objRowFill = New System.Drawing.Rectangle(1, 1, DG.Width - 2, intRowHeight - 2)
        objGraphics.FillRectangle(System.Drawing.SystemBrushes.Window, objRowFill)
        objRowHeaderFrame = New System.Drawing.Rectangle(2, 2, intRowHeadersWidth - 2, intRowHeight - 4)
        objGraphics.FillRectangle(New System.Drawing.SolidBrush(DG.RowHeadersDefaultCellStyle.BackColor), objRowHeaderFrame)
        intLeftOffset = intRowHeadersWidth
        For Each objColumn As System.Windows.Forms.DataGridViewColumn In DG.Columns
            If objColumn.Visible Then
                intLeftOffset += objColumn.Width
                objGraphics.DrawLine(System.Drawing.SystemPens.ControlDark, New System.Drawing.Point(intLeftOffset, 0), New System.Drawing.Point(intLeftOffset, intRowHeight))
            End If
        Next objColumn
        intIndex = (DG.Height - intTopOffset) \ intRowHeight
        For j As Integer = 0 To intIndex
            e.Graphics.DrawImage(objRowImage, 0, intTopOffset + j * intRowHeight)
        Next j
    End If
End Sub

我得到的错误是“无法调用'indexOf',其参数类型为'(Card)'”。我根本不明白这一点。交易是[卡]。如果不是卡,我应该怎么调用deal.indexOf? docs中CollectionType.indexOf的签名是

func indexOf(element:Self.Generator.Element) - >手动调用self.index?

如果交易是[卡]不是Self.Generator.Element等于卡?我甚至输入类型注释来检查我的变量是否具有我期望的类型。我错过了什么? (碰巧卡片是一个结构体,但如果我把它改成一个类,我会得到同样的错误。)

1 个答案:

答案 0 :(得分:6)

问题在于您尚未定义一张卡与另一张卡相同的含义。因此,Swift无法知道给定卡的索引在数组中是什么,因为它在看到它时无法识别所需的卡。您需要将Card声明为Equatable并满足该协议的要求。