2

i would like to change my swift code to swift 2.0 and i hope you can help me with this.

                let calendar = NSCalendar.currentCalendar()
            let components = calendar.components(.CalendarUnitYear | .CalendarUnitMonth | .CalendarUnitDay , fromDate:  notification.fireDate!)
            var gregorian:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
            let unit: NSCalendarUnit = .CalendarUnitHour | .CalendarUnitMinute
            var comps:NSDateComponents = gregorian.components(unit, fromDate: timepicker.date)


            comps.setValue(components.day, forComponent: NSCalendarUnit.CalendarUnitDay)
            comps.setValue(components.month, forComponent: NSCalendarUnit.CalendarUnitMonth)
            comps.setValue(components.year, forComponent: NSCalendarUnit.CalendarUnitYear)
            comps.setValue(comps.hour, forComponent: NSCalendarUnit.CalendarUnitHour)
            comps.setValue(comps.minute, forComponent: NSCalendarUnit.CalendarUnitMinute)
            var NewFiredate : NSDate = gregorian.dateFromComponents(comps)!

in this part of my code i get the following errors:

  • Could not find member 'CalendarUnitYear'
  • Could not find member 'CalendarUnitHour'
  • 'NSCalendarUnit.Type' does not have a member named 'CalendarUnitHour'
  • 'NSCalendarUnit.Type' does not have a member named 'CalendarUnitMinute'

thank you very much :)

Trombone0904
  • 4,132
  • 8
  • 51
  • 104

1 Answers1

3

You can use the code below, basically I replaced the enums with new OptionSetType syntax and few small changes. NSCalendarUnit OptionSetType has these static types which you can use,

static var Era: NSCalendarUnit { get }
static var Year: NSCalendarUnit { get }
static var Month: NSCalendarUnit { get }
static var Day: NSCalendarUnit { get }
static var Hour: NSCalendarUnit { get }
static var Minute: NSCalendarUnit { get }
static var Second: NSCalendarUnit { get }
static var Weekday: NSCalendarUnit { get }
static var WeekdayOrdinal: NSCalendarUnit { get }
static var Quarter: NSCalendarUnit { get }
static var WeekOfMonth: NSCalendarUnit { get }
static var WeekOfYear: NSCalendarUnit { get }
static var YearForWeekOfYear: NSCalendarUnit { get }
static var Nanosecond: NSCalendarUnit { get }
static var Calendar: NSCalendarUnit { get }
static var TimeZone: NSCalendarUnit { get }

So, you new code would look like this,

let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Year, .Month, .Day] , fromDate:  notification.fireDate!)
var gregorian:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let unit: NSCalendarUnit = [.Hour, .Minute]
var comps:NSDateComponents = gregorian.components(unit, fromDate: timepicker.date)


comps.setValue(components.day, forComponent:.Day)
comps.setValue(components.month, forComponent:.Month)
comps.setValue(components.year, forComponent:.Year)
comps.setValue(comps.hour, forComponent:.Hour)
comps.setValue(comps.minute, forComponent:.Minute)
var NewFiredate : NSDate = gregorian.dateFromComponents(comps)!
Sandeep
  • 20,908
  • 7
  • 66
  • 106
  • thank you. but xcode tell me, that i have change the first line to _ = NSCalendarUnit.Year – Trombone0904 Jul 31 '15 at 11:44
  • Oh yes remove the first line, I will edit it. – Sandeep Jul 31 '15 at 11:45
  • 1
    _ means that you are not using the variable, so the compiler wants to set it to any variable name. Since, we are not using, it just does not matter what the name is, so xcode tries to make it _. I removed the line now. – Sandeep Jul 31 '15 at 11:46
  • okay but what mean "_ =" ? i have to set this in another part of my code, too – Trombone0904 Jul 31 '15 at 11:46
  • 1
    It just means that if you do not care what the value is and do not want to use it further you simply assign it _. So, it is like AnyObject for object, any name undefined name or anything you can say it. – Sandeep Jul 31 '15 at 11:47
  • @GeneratorOfOne Helpful comments but I wouldn't say that `_` is like `AnyObject` for an object. They're completely unrelated concepts :) – Antonio Favata Aug 06 '15 at 13:25