arrays


Need someone to explain this code [closed]


I have been trying to understand this:
var arrayOfLabels =
[
"Hello",
"Hey",
"Hi",
"Howdy"
]
#IBOutlet weak var labelHere: UILabel!
var currentElementIndex = 0
#IBAction func clickForNextElement(_ sender: UIButton) {
currentElementIndex += 1
let numberOfElements = arrayOfLabels.count // arrayOfLabels.count = Amount of elements in arrayOfLabels
let nextElement = currentElementIndex % numberOfElements
labelHere.text = arrayOfLabels[nextElement]
}
I don't get what % does, what the constant "numberOfElements" & "nextElement" does.. I do get a little bit, like i would be able to code something similar to this without understanding it. That's why i need some simple, detailed explanation.
Thank you!
numberOfElements is set to the number of items in the arrayOfLabels. You have five items in that array so numberOfElements is set to 5.
The percent sign "%" is called the modulo operator. What it does is tells you the remainder after integer division. So, for example, if you write 5 % 3 what that does is recognizes that 3 will go into 5 only once with a remainder of 2 so 5 % 3 = 2.
In the context of the code currentElementIndex gets increased by one every time you push the button. So it starts at 0, then goes to 1, 2 etc.
When currentElementIndex is less than numberOfElements then currentElementIndex % numberOfElements will be the same as currentElementIndex. You can check that yourself - for example try when currentElementIndex is 2. 2 % 5 says "five goes into two zero times with a remainder of 2" so 2 % 5 == 2 and nextElement will be 2
When currentElementIndex grows large enough to be equal to a multiple of the number of items in the list, you want it to start over. The first time that happens is when currentElementIndex == numberOfElements the values would be 5 and 5. 5 % 5 says "5 goes into 5 one time with a remainder of 0". So 5 % 5 = 0 and your 'nextElement` becomes zero.
The second time it happens would be when currentElementIndex grows to be 10. In that case 10 % 5 say "five goes into 10 twice with a remainder of zero" Again nextElement rolls back to zero.
Try putting print statements into the code and watch how both currentElemetnIndex and nextElement change when you push the buttons/
Here's a commented version of the code:
// 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]
}

Related Links

In Ruby, how can I iterate over an arbitrary number of arrays?
How do I get the results of a split returned in the order in which they occur in the split?
Long Array VBA Issue
Inserting matrix into array for every cell of a raster and getting dimensions right
How to check that datatype of json response is of array type in postman?
Assembly fill array with Consecutive numbers
Pass an array of strings into a function without having to specify N (compile time constant)
How to Get and Set a string's character at a specific index in Swift
Unexpected output in bash script. Instead of the literal “?”, there is echoed “d” and “i”
Relating a key of a hash to an array element in a different array in Ruby
Ruby pushing strings to an array
check hash value within an array
Unable to pass array to method in Ruby
Excel VBA - array used to process ranges changes the format of the cells
Typescript object extends the Array<BoxModel> can not call the inside methods anymore
Excel VBA - Checking array from adjacent cell

Categories

HOME
system-verilog
youtube-livestreaming-api
windows-store-apps
apollo
phaser-framework
hashicorp-vault
dryioc
data-synchronization
flann
activesync
react-leaflet
lstm
rhapsody
grub2
anychart
ng-show
zend-framework-mvc
backpropagation
excel-2010
countif
iup
explode
azure-servicebus-queues
excel-2013
pcre
configure
blackboard
vegan
canvasjs
pycparser
modulo
hashcat
color-scheme
declare
initializer
linq-to-entities
polyfills
soundjs
twiki
common.logging
ical-dotnet
nashorn
xbim
homekit
startapp
archer
aqgridview
chown
sapui
istorage
tiddlywiki
multi-agent
ogre3d
windows-95
gce
static-code-analysis
communication-protocol
bettercms
lotus
restivejs
efxclipse
rsqlite
payu
picasa
multifile-uploader
gradle-eclipse
dac
yoothemes
approval-tests
throughput
evo
mogrify
stream-framework
playscape
graphical-logo
google-plus-one
adcolony
docopt
outline
dache
colt
ifft
initialization-vector
healthvault
modeshape
asp.net-mvc-scaffolding
trimming
ng-pattern
google-closure-library
qtgui
qt4.6
arbor.js
android-4.0
throttling
ou
curljs
port-scanning
search-path
multibyte-functions
scatterview

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App