我在我的应用程序中收到 Use of unresolved identifier 'self'
。看完this link ,我将代码包装在一个类中。
注意:我正在使用从 answer 获得的扩展在 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)
}
我该如何解决这个问题?
最佳答案
我已经创建了一个新的扩展来为您将偏移分量输出为字符串:
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)
}
}
测试
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()
关于ios - 如何在我的标签上显示两个日期的时间偏移量(年、月、周...)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30835148/