根据循环次数选择数组值

时间:2016-08-25 14:39:29

标签: ios arrays swift

我有一个数组,我需要根据循环次数从数组中选择一个值。它遵循一种模式。对于循环1,常量定义为每个数组索引。如果循环增加了" 1",那么常量将通过将数组索引移动1来获得数组的值。我做了以下操作,我不知道如何简化代码。请帮助!!

var cycle = 1
let myStr: [String] = ["A", "B", "C", "D", "E", "F", "G", "H", "I"]

if cycle == 1{
let a = myStr[0]; let b = myStr[1]; let c = myStr[2]; let d = myStr[3]; let e=myStr[4]; let f = myStr[5]; let g = myStr[6]; let h = myStr[7]; let i = myStr[8]
}else if cycle == 2{
  let a = myStr[8]; let b = myStr[0]; .... let i = myStr[7]
}else if cycle == 3{
  let a = myStr[7]; let b = myStr[8]; ....  let i = myStr[6]
}else if cycle == 4{
  let a = myStr[6]; let b = myStr[7]; .... let i = myStr[5]
}else if cycle == 5{
  let a = myStr[5]; let b = myStr[6]; .... let i = myStr[4]
}else if cycle == 6{
  let a = myStr[4]; let b = myStr[5]; .... let i = myStr[3]
}else if cycle == 7{
  let a = myStr[3]; let b = myStr[4]; ....  let i = myStr[2]
}else if cycle == 8{
  let a = myStr[2]; let b = myStr[3]; ....  let i = myStr[1]
}else if cycle == 9{
  let a = myStr[1]; let b = myStr[2]; .... let i = myStr[0]
}

在按钮操作中,我有以下

    cycle = cycle + 1
    if cycle > 9{
        cycle = 1
    }

0 个答案:

没有答案