[Swift 1] Day 10 - Code Exercise Solution


#21

I updated my code in Github. Have fun!

func playWithText(date: NSDateComponents)->(String, String, String){
        var year : String = "year"
        var month : String = "month"
        var day : String = "day"
        
        if date.year % 2 == 0 {
            year = "years"
        }
        
        if date.month % 2 == 0 {
            month = "months"
        }
        
        if date.day % 2 == 0 {
            day = "days"
        }
        
        return (year,month,day)
    }
func negativeTimer (component : NSDateComponents, var str : String) -> (NSDateComponents,String) {
        if component.second < 0 {
            component.second *= -1
            component.minute *= -1
            component.hour *= -1
            component.day *= -1
            component.month *= -1
            component.year *= -1
            str = "From:"
        }
        return (component,str)
    }

And in updateTimer():

func updateTimer(){
        var date = fromDateToDate(datePicker.date)
        let (year, month, day) = playWithText(date)
        var str: String = ""
        (date,str) = negativeTimer(date, str: str)
        yearMonthDayLabel.text = "\(str) \(date.year)\(year)  \(date.month)\(month)  \(date.day)\(day)"
        timeLabel.text = "\(date.hour):\(date.minute):\(date.second)"
    }