ios - Incrementing Array Element By One In Swift -
i working on swift project in xcode, , i've encountered issue cannot increment array's 1 element through various endeavours.
my viewcontroller.swift
import uikit class failureviewcontroller: uiviewcontroller { @iboutlet weak var failfactlabel: uilabel! let failfact = failure() override func viewdidload() { super.viewdidload() failfactlabel.text = failfact.failarray[0] } @ibaction func showfunfact() { //this issue } }
for function showfunfact have tried
for var x = 0; x < failarray.cout; x++ { failfactlabel.text = failfact.failarray[x] }
but encountered error: "use of unresolved identifier".
putting aside, decided use
for var x = 0; x < 10; { x+=1 }
although not generate error however, see stops @ first element in array. tried +=5 , displays 5th element in array once, believe code runs through once. need consistently working piece continuously displays next element, stumped because should theoretically work since called "loop" or suggestion appreciated!
objective redefined: trying show 1 fact @ time, before have used random function generate random fact every time things tend repetitive , therefore decided start on initial array index 0 , grab next fact (next element in array) every time button pressed (thanks luk's question)
you need create instance variable hold current factindex, if trying:
import uikit class failureviewcontroller: uiviewcontroller { @iboutlet weak var failfactlabel: uilabel! var factindex = 0 let failfact = failure() override func viewdidload() { super.viewdidload() failfactlabel.text = failfact.failarray[factindex] } @ibaction func showfunfact() { factindex++ if factindex >= failfact.failarray.count { factindex = 0 } failfactlabel.text = failfact.failarray[factindex] } }
Comments
Post a Comment