I think it's pretty common problem, but I haven't found the solution. Here is the code:
class ViewController: UIViewController {
@IBOutlet weak var monthLabel: UILabel!
let date = Date()
let calendar = NSCalendar.current
let month = calendar.component(.month, from: date)
//let month = calendar.component(.month, from: date)
//let day = calendar.component(.weekday, from: date)
//let dateDay = calendar.component(.day, from: date)
override func viewDidLoad() {
super.viewDidLoad()
// some code
}
@objc func addStory(sender: UIButton!) {
print("Button tapped")
self.performSegue(withIdentifier: "addStory", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "addStory"{
let destinationVC = segue.destination as! ResultsViewController
// destinationVC.currentDayKey =
}
}
}
When I try to build it I get the "Cannot use instance member 'calendar' within property initializer; property initializers run before 'self' is available" and "Cannot use instance member 'date' within property initializer; property initializers run before 'self' is available"
But if I initialise month, day and dateDay in viewDidLoad everything works. How to declare them on the top?
I've tried to find some solutions with same error, but nothing helped