이 코드를 설명 할 사람이 필요해 약간, 나는 그것을 이해하지 않고 이와 비슷한 것을 코드화 할 수있을 것입니다. 그래서 간단하고 상세한 설명이 필요한 것입니다.내가 이것을 이해하기 위해 노력 해왔다
감사합니다.
이 코드를 설명 할 사람이 필요해 약간, 나는 그것을 이해하지 않고 이와 비슷한 것을 코드화 할 수있을 것입니다. 그래서 간단하고 상세한 설명이 필요한 것입니다.내가 이것을 이해하기 위해 노력 해왔다
감사합니다.
numberOfElements
이 arrayOfLabels
의 항목 수로 설정됩니다. 해당 배열에 다섯 개의 항목이 있으므로 numberOfElements
이 5로 설정됩니다.
백분율 기호 "%"를 모듈러스 연산자라고합니다. 그것은 정수 나누기 후에 나머지를 알려줍니다. 예를 들어, 5 %를 쓰는 경우 3은 3이 2의 나머지와 함께 5로 한 번만 들어가므로 5 % 3 = 2를 인식합니다.
currentElementIndex가 당신이 버튼을 누를 때마다 하나씩. 은 0에서 시작하므로, 그 후, 1 간다 2 등
currentElementIndex
currentElementIndex
가 동일한 것보다 numberOfElements
다음 currentElementIndex % numberOfElements
currentElementIndex
5 말한다 2. 2 % 일 때 시도 그래서 예를 들면 2 % "오 2의 나머지 두 제로 시대로 간다"
currentElementIndex
가 성장하면 5 == 2
nextElement
2
될 것입니다 목록에있는 항목 수의 배수와 같을만큼 큰 경우 다시 시작해야합니다. 처음 발생하는 것은 currentElementIndex
== numberOfElements
일 때 값은 5와 5가됩니다. 5 % 5는 "5가 한 번에 5가되고 나머지는 0"이라고 말합니다. 따라서 5 % 5 = 0이고 'nextElement'는 0이됩니다.
currentElementIndex가 10이 될 때 두 번째로 발생합니다.이 경우 10 %는 5가 "0이 나머지는 0으로 두 번 10"이됩니다. 다시 nextElement가 0으로 롤백됩니다.
봅니다 코드로 인쇄 문을 넣고 모두 currentElemetnIndex
및 nextElement
변화가 당신이 버튼을 누르면 어떻게 볼/
다음은 코드의 주석 버전입니다 :
// create an array of labels
var arrayOfLabels =
[
"Hello",
"Hey",
"Hi",
"Howdy"
]
// a variable for the label
@IBOutlet weak var labelHere: UILabel!
// initialise a count
var currentElementIndex = 0
// create a function
@IBAction func clickForNextElement(_ sender: UIButton) {
// Add one to our count
currentElementIndex += 1
// set numberOfElements to be how many elements are in the array (i.e. 4)
let numberOfElements = arrayOfLabels.count // arrayOfLabels.count = Amount of elements in arrayOfLabels
// use the % to work out remainder when dividing our ongoing count by the number of elements
let nextElement = currentElementIndex % numberOfElements
// set the label text to the value of the remainder-th element
labelHere.text = arrayOfLabels[nextElement]
}
오 그것의 [모듈] (https://en.wikipedia.org/wiki/Modulo_operation). 클릭 할 때마다 currentElementIndex를 1 씩 늘리므로 배열에서 벗어날 수 있습니다. 모듈러스가이를 방지합니다. – Raymond
알림 연산자 https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html – Slai