0

I am getting Use of unresolved identifier 'self' in my app. After reading this link, I wrapped the code in a class.

Note: I am using an extension I got from this answer at S.O.

class SetDate {

    var seconds = date2.secondsFrom(date1)
    var minutes = date2.minutesFrom(date1)
    var hours = date2.hoursFrom(date1)
    var days = date2.daysFrom(date1)
    var weeks = date2.weeksFrom(date1)
    var months = date2.monthsFrom(date1)
    var years = date2.yearsFrom(date1)

    func setLabels(seconds,minute,hous,weeks,days,months,years) {
        self.SLabel.text = s
        self.MLabel.text = min
        self.HLabel.text = h
        self.WLabel.text = w
        self.DLabel.text = d
        self.MLabel.text = m
        self.YLabel.text = y
    }

    setLabels(seconds,minutes,hours,weeks,days,months,years)
}

How can I resolve the issue?

Community
  • 1
  • 1
WebRuin
  • 127
  • 1
  • 3
  • 10
  • Where are `SLabel`, `MLabel`, `HLabel`, `WLabel`, `DLabel`, `MLabel`, and `YLabel` defined? – NobodyNada Jun 14 '15 at 22:25
  • Forget about that. You don't need to use self to accomplish what you want. Do whatever you want inside your view controller class – Leo Dabus Jun 14 '15 at 22:29
  • https://gist.github.com/WebRuin/1dd4c09a66a024a2a787 – WebRuin Jun 14 '15 at 22:30
  • You should implement a method instead of a class – Leo Dabus Jun 14 '15 at 22:33
  • method or class, I still get the error. – WebRuin Jun 14 '15 at 22:39
  • http://i.imgur.com/11F2TkO.png – WebRuin Jun 14 '15 at 22:43
  • Somebody edited the subject of the question. If you really want to display a time offset, you should not compute the individual components as strings. You should use [NSDateComponentsFormatter](https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSDateComponentsFormatter_class/index.html) to format a difference between two dates to a string. – Ken Thomases Jun 15 '15 at 04:08

2 Answers2

1

I have created a new extension to output the offset components as string for you:

import UIKit
extension NSDate {
    func yearsFrom(date:NSDate) -> Int{
        return NSCalendar.currentCalendar().components(.CalendarUnitYear, fromDate: date, toDate: self, options: nil).year
    }
    func monthsFrom(date:NSDate) -> Int{
        return NSCalendar.currentCalendar().components(.CalendarUnitMonth, fromDate: date, toDate: self, options: nil).month
    }
    func weeksFrom(date:NSDate) -> Int{
        return NSCalendar.currentCalendar().components(.CalendarUnitWeekOfYear, fromDate: date, toDate: self, options: nil).weekOfYear
    }
    func daysFrom(date:NSDate) -> Int{
        return NSCalendar.currentCalendar().components(.CalendarUnitDay, fromDate: date, toDate: self, options: nil).day
    }
    func hoursFrom(date:NSDate) -> Int{
        return NSCalendar.currentCalendar().components(.CalendarUnitHour, fromDate: date, toDate: self, options: nil).hour
    }
    func minutesFrom(date:NSDate) -> Int{
        return NSCalendar.currentCalendar().components(.CalendarUnitMinute, fromDate: date, toDate: self, options: nil).minute
    }
    func secondsFrom(date:NSDate) -> Int{
        return NSCalendar.currentCalendar().components(.CalendarUnitSecond, fromDate: date, toDate: self, options: nil).second
    }

    func offsetComponentsFrom(date:NSDate) -> (years:String,months:String,weeks:String,days:String,hours:String,minutes:String,seconds:String) {

        let yearsFromDate = yearsFrom(date)
        let years = "\(yearsFromDate) year" + {return yearsFromDate > 1 ? "s" : ""}()

        let monthsFromDate = monthsFrom(date)
        let months =  "\(monthsFromDate) month" + {return monthsFromDate > 1 ? "s" : ""}()

        let weeksFromDate = weeksFrom(date)
        let weeks = "\(weeksFromDate) week" + {return weeksFromDate > 1 ? "s" : ""}()

        let daysFromDate = daysFrom(date)
        let days =  "\(daysFromDate) day" + {return daysFromDate > 1 ? "s" : ""}()

        let hoursFromDate = hoursFrom(date)
        let hours =  "\(hoursFromDate) hour" + {return hoursFromDate > 1 ? "s" : ""}()

        let minutesFromDate = minutesFrom(date)
        let minutes = "\(minutesFromDate) minute" + {return minutesFromDate > 1 ? "s" : ""}()

        let secondsFromDate = secondsFrom(date)
        let seconds = "\(secondsFromDate) second" + {return secondsFromDate > 1 ? "s" : ""}()
        return (years,months,weeks,days, hours, minutes, seconds)
    }
}

Testing

let date1 = NSCalendar.currentCalendar().dateWithEra(1, year: 2014, month: 11, day: 28, hour: 5, minute: 9, second: 0, nanosecond: 0)!
let date2 = NSCalendar.currentCalendar().dateWithEra(1, year: 2015, month: 8, day: 28, hour: 5, minute: 9, second: 0, nanosecond: 0)!

func setLabels() {
    let components = date2.offsetComponentsFrom(date1)
    let years = components.years       // "0 year"
    let months = components.months     // "9 months"
    let weeks = components.weeks       // "39 weeks"
    let days = components.days         // "273 days"
    let hours = components.hours       // "6553 hours"
    let minutes = components.minutes   // "393180 minutes"
    let seconds = components.seconds   // "23590800 seconds"

    // set your labels
    //
    // yearsLabel.text = years
    // monthsLabel.text = months
    // weeksLabel.text = weeks
    // daysLabel.text = days
    // hoursLabel.text = hours
    // minutesLabel.text = minutes
    // secondsLabel.text = seconds

}

setLabels()
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
0

You need to first declare your variables as class variables and give its values inside a function

class SetDate {

var seconds = Int()
var minutes = Int()
var hours = Int()
var days = Int()
var weeks = Int()
var months = Int()
var years = Int()

func setLabels(seconds,minute,hous,weeks,days,months,years) {
    self.SLabel.text = String(seconds)
    self.MLabel.text = String(minute)
    self.HLabel.text = String(hous)
    self.WLabel.text = String(weeks)
    self.DLabel.text = String(days)
    self.MLabel.text = String(months)
    self.YLabel.text = String(years)
}

func populateVars(){    
    seconds = date2.secondsFrom(date1) 
    minutes = date2.minutesFrom(date1)
    hours = date2.hoursFrom(date1)
    days = date2.daysFrom(date1)
    weeks = date2.weeksFrom(date1)
    months = date2.monthsFrom(date1)
    years = date2.yearsFrom(date1)

    setLabels(seconds,minutes,hours,weeks,days,months,years)
}

}

As @LeoDabus indicates date2.yearsFrom returns a Int not an NSDate, so I change the declaration to fix it.

Icaro
  • 14,585
  • 6
  • 60
  • 75
  • 1
    Fala Icaro . date2.secondsFrom(date1) é uma extensao que eu postei em outra pergunta. It returns an Int not a NSDate – Leo Dabus Jun 14 '15 at 23:05
  • Thanks, the is much cleaner! :) I am still getting all the errors. – WebRuin Jun 14 '15 at 23:05
  • that is because swift does not know what NSDate is import the framework to your class it will fix it – Icaro Jun 14 '15 at 23:07
  • 1
    Fala @LeoDabus, eu vou modificar a resposta para ajustar com sua func. Obrigado por me avisar – Icaro Jun 14 '15 at 23:09
  • import Foundation import NSDate gives me 'no such module NSDate – WebRuin Jun 14 '15 at 23:50
  • It seems that you don't need NSDate as indicate by @LeoDabus the functions return Int, I did the necessary changes above, also in my copy and past I declare the variable twice the function should not have the word var before the variable. – Icaro Jun 15 '15 at 00:07
  • I did copy and paste – WebRuin Jun 15 '15 at 00:40
  • 1
    @Icaro you can't assign an Int to an UILabel.text property – Leo Dabus Jun 15 '15 at 00:49
  • 1
    @LeoDabus We forget so many things when you not in the IDE, thanks mate, I think you already did a good job in your answer. I just fix that and leave this here (after all the work). – Icaro Jun 15 '15 at 00:53