Raghav
Raghav

Reputation: 114

Error - Instance member cannot be used on type custom class

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'

Upvotes: 1

Views: 2233

Answers (1)

Leo Dabus
Leo Dabus

Reputation: 236260

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

Upvotes: 2

Related Questions