【问题标题】:NSDate how to add different years/month in swift 2.2NSDate如何在swift 2.2中添加不同的年份/月份
【发布时间】:2016-09-19 20:48:51
【问题描述】:

UITextField 中,我输入了一个加上 43 年零 2 个月的日期 我会确保截至 2016 年 1 月 1 日,总和为 43 年 2 个月 01/01/2020 从总和变成 44 年零 5 个月 01/01/2022 从总和变成 44 年零 8 个月 等等

这是我的代码 谁能帮帮我。

func upDateField(sender: UIDatePicker){


    inizioTextField.text = dateFormat.stringFromDate(sender.date)


    let currentDate = NSDate()

    let dateFormatter = NSDateFormatter()

    dateFormatter.locale = NSLocale.currentLocale()
    dateFormatter.locale = NSLocale(localeIdentifier: "it_IT")

    dateFormatter.dateStyle = NSDateFormatterStyle.LongStyle
    let strDate = dateFormatter.stringFromDate(datePicker.date)
    inizioTextField.text = strDate

    let yearsToAdd = 43
    let monthsToAdd = 2


    let newDateComponents = NSDateComponents()
    newDateComponents.year = yearsToAdd
    newDateComponents.month = monthsToAdd

    let calculatedDate = NSCalendar.currentCalendar().dateByAddingComponents(newDateComponents, toDate: datePicker.date, options: NSCalendarOptions.init(rawValue: 0))

    let resultDate = dateFormatter.stringFromDate(calculatedDate!)
    dateFormatter.dateStyle = NSDateFormatterStyle.LongStyle
    anticipoLabel.text =  "se tutto va bene dal \(resultDate) sei in pensione anticipata!"
    anticipoLabel.alpha = 1

}

感谢您的回答 我试图这样做,但我得到了这个错误:无法转换“NSDate”类型的值?到预期的参数类型 Int 在:case_where 计算日期

我哪里错了? 这是修改后的代码。

var yearsToAdd = 0 varmonthToAdd = 0

    let newDateComponents = NSDateComponents()
    newDateComponents.year = yearsToAdd
    newDateComponents.month = monthsToAdd

    let calculatedDate = NSCalendar.currentCalendar().dateByAddingComponents(newDateComponents, toDate: datePicker.date, options: NSCalendarOptions.init(rawValue: 0))

    switch calculatedDate {
    case _ where calculatedDate < 2016 :
        yearsToAdd = 43
        monthsToAdd = 2

    case _ where calculatedDate < 2020 :
        yearsToAdd = 50
        monthsToAdd = 2

    default:
        break

    }

【问题讨论】:

  • 你能说得更清楚些吗?
  • 那怎么了?您遇到了什么行为,这与您的预期有何不同?
  • 顺便说一句,设置locale 两次是没有意义的。 (坦率地说,我认为设置locale 根本没有任何目的,而是让它默认为用户在“设置”中指定的任何语言环境)。也不设置dateStyle 两次。即使dateStyle 在第二次出现时有所不同,设置dateStyle 之后你已经从日期构建字符串是没有意义的。

标签: ios swift2 nsdate nsdatecomponents


【解决方案1】:

如果我正确理解您的问题,您想根据当前年份添加不同的值,对吗?为了实现这一点,您需要这样的东西:

let currentYear = NSCalendar.currentCalendar().component(.Year, fromDate: NSDate())

var yearsToAdd = 0
var monthsToAdd = 0

switch currentYear {
    case _ where currentYear < 2016 :
        yearsToAdd = 43
        monthsToAdd = 2
    case _ where currentYear < 2020 :
        // ... and so on
}

case Swift 中的语句不会自动失败。
请记住,这是一种非常幼稚的方法。谷歌翻译说你的代码示例中的字符串说的是“提前退休” - 也许你的立法中有一个公式来计算偏移量?

【讨论】:

  • 那里应该有空格 - case _ where,而不是 case_where_ 告诉编译器您不关心值,而是关心比较。
  • 您需要从日期对象中提取年份,如我的回答所示。
  • 好的,再次感谢,没有更多错误,但现在发生的不是总和,而是施加抱歉的日期,但我仍然需要您的帮助
  • 对不起,我不明白你的意思。
  • 我输入了一个日期,但只对输入的日期求和,但我发现我的代码有错误,谢谢,现在可以正常工作了
猜你喜欢
  • 2020-03-28
  • 1970-01-01
  • 1970-01-01
  • 2011-05-06
  • 1970-01-01
  • 2019-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多