1

I'm trying to get today's date

import Foundation

class Date {
  var calendar = NSCalendar.currentCalendar()
  var day = calendar.component(NSCalendarUnit.Day, fromDate: NSDate())
}

But I keep getting error

Instance member 'calendar' cannot be used on type 'Date'

1 Answer 1

2

You can't access calendar property at instantiation time:

Try like this:

class Date {
    let calendar = NSCalendar.currentCalendar()
    var day: Int {
       return  calendar.component(.Day, fromDate: NSDate())
    }
}


print(Date().day)   // 17
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.