Word of the day concept- While loop issue

Hello,


I wrote a code using swift in xCode to generate a new word from an array each day, so when the system time is 12 am it takes another variable from the array; like "word of the day" in the Dictionary app. So when i test it as print statments it shows the perfect result i want in the console. But when map/assign it to the label in the viewController to be shown in the simulator it doesnt work.

I did some reaseches regarding this issue and it appears that i cant use an "infinte loop" using while, if or for.

I also tried many ways including making the loop a for loop with the size 3, in the simulator it doesnt open the application home page unless the loop is done then the page appears with the 3rd word printed.



Can anyone please help me with this issue.



Regards,

Asma.

If you want help fixing code, you have to post the code that isn't working.

If you start an infinite loop in the main thread of your application, then the user interface (the label) will not have a chance to be drawn after you set it (since the user interface also runs on the main thread, and your infinite loop will never let go of it).


I can't see why you would want an infinite loop anywhere in order to show a word (even if it should change once a day). It's not like you have to loop to keep the app alive, that's already taken care of by the app's runloop. What you would do instead is to check and schedule checks to see if it's time to change the word or not (using something like eg dispatch_after). These checks can be made very rarely, eg when the app becomes active/visible it can check the time, and if it's 6 hours until 12 am, then it can schedule the next check to be in 6 hours (should the app be active/visible in 6 hours, otherwise the app will remove the scheduled check on exit / hide).

var i=0


while i<3 {

now = NSDate()

calendar = NSCalendar.currentCalendar()

nowComponents = calendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitSecond | .CalendarUnitDay | .CalendarUnitMonth | .CalendarUnitYear , fromDate: now)

nowyear = nowComponents.year

nowmonth = nowComponents.month

nowhour = nowComponents.hour

nowminutes = nowComponents.minute

nowsecond = nowComponents.second

nowday=nowComponents.day

if ( nowyear==nextDayyear && nowmonth==nextDaymonth && nowday==nextDayday && nowhour==nextDayhour && nowminutes==nextDayminutes && nowsecond==nextDaysecond ) //i tried to use if nextDay.isEqualToDate(now) it doesnt work

{

now = NSDate()

calendar = NSCalendar.currentCalendar()

nowComponents = calendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitSecond | .CalendarUnitDay | .CalendarUnitMonth | .CalendarUnitYear , fromDate: now)

nowyear = nowComponents.year

nowmonth = nowComponents.month

nowhour = nowComponents.hour

nowminutes = nowComponents.minute

nowsecond = nowComponents.second

nowday=nowComponents.day


reminderhour=23-nowhour

reminderhourssecond=reminderhour*3600

x=60-nowminutes

reminderminutessecond=x*60

totalreminertimeseconds=reminderhourssecond+reminderminutessecond

var totaltimefornextday: NSTimeInterval = NSTimeInterval(totalreminertimeseconds)


nextDay = NSDate(timeIntervalSinceNow: 2 )

nextDaycalendar = NSCalendar.currentCalendar()

nextDayComponents = nextDaycalendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitSecond | .CalendarUnitDay | .CalendarUnitMonth | .CalendarUnitYear , fromDate: nextDay)

nextDayyear = nextDayComponents.year

nextDaymonth = nextDayComponents.month

nextDayhour = nextDayComponents.hour

nextDayminutes = nextDayComponents.minute

nextDaysecond = nextDayComponents.second

nextDayday=nextDayComponents.day

Label.text = array[i]

i++

}

}

I just posted the code as a reply. Thank you

Actually i dont know how swift works its my first project using it. But i tried to print out the nsdate in diffrent time it doesnt change thats why i followed this procedure. I didnt use or hear about the schedule checks or dispatch_after could you please refer to me any source that provides an explination for this? Cause i can rarley find and they are not very helpful.

I posted my code below if you have any comment about.

Thank you so much.

It is perfectly reasonable to have these kinds of infinite loops in process based languages/frameworks, just not in Cocoa, since it's not a process based framework.


Process based (functional):

func loop() {
    doSomething()
    sleepUntilEvent()
    loop()
}


(Do not write code like this in Swift, it is not guaranteed to be tail-call optimized. Write it with a while loop instead. I just wanted to show the similarites between the approaches.)


Event trigger based:

func eventTriggered() {
    doSomething()
    installTrigger {
        eventTriggered()
    }
}


So, in Swift/Cocoa the event trigger based method would be used.

Long story short: The user interface will not be updated within/during your loop, so you'll only see the last value that was assigned to Label.text (array[2]).


I saw someone posting a link to some "getting started with iOS development" video tutorials or something, you might try and find those.

Word of the day concept- While loop issue
 
 
Q