2015-12-04 191 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() 
} 

}