2015-12-04 453 views
2

我的功能是下面的那個 我試圖在數組達到極限時禁用文本。 我收到數組超出範圍的錯誤。我可以使用什麼類型的條件,以便在array1.count等於swipeCount時禁用陣列。 這是我的代碼:啓用禁用輕掃手勢swift

let array1 = ["a","b","c","d"] 

func getRandom1() { 
    for var i = 0; i < array1.count ; i++ 
    { 
     array1.shuffle1() 
    } 

} 

func getText1() { 

    self.display.text = "\(array1[i++])" 
    swipeCount++ 
} 

func getTextBack() { 


    self.display.text = "\(array1[i])" 

} 


func handleSwipes(sender:UISwipeGestureRecognizer) { 

if (sender.direction == .Right) 
{ 

    if swipeCount != array1.count 
    { 
     getText1() 
    } 


    else 

    { 
     getTextBack() 
    } 

    } 


    } 
+1

什麼是'array1'? – JAL

+0

array1包含所有文本,如let array1 = [「a」,「b」,「c」,「d」] –

+0

@SabhaySardana:我沒有看到任何可能導致數組超出範圍的代碼。請告訴我們如何定義'getText1()'和'notText()'。 –

回答

0

改變這一行:

if swipeCount != array1.count 

if swipeCount < array1.count - 1 

我認爲你不能在getText1和getTextBack使用我。而不是使用我的,你應該使用swipeCount像這樣:

func getText1() { 
    self.display.text = "\(array1[swipeCount++])" 
} 

func getTextBack() { 
    self.display.text = "\(array1[swipeCount])" 
} 
+0

我已經嘗試過,我剛剛更新我的代碼。你可以看到,在我的編輯:) –

1
func handleSwipes(sender:UISwipeGestureRecognizer) { 

if (sender.direction == .Right) { 
    let aCount = array1.count - 1 

    if swipeCount < aCount 
{ 
    getText1() 
} 


else 

{ 
    getTextBack() 
} 

}