How to convert NSDate to NSString in Swift? (Detailed guide)
Hello everyone! I'm new to Swift and encountered a problem when working with dates. I need to display an NSDate object as a string in a UILabel, but I can't figure out how to do it correctly.\n\nI have this code:\n\n```swift\nimport UIKit\n\nclass ViewController: UIViewController {\n @IBOutlet weak var dateLabel: UILabel!\n \n override func viewDidLoad() {\n super.viewDidLoad()\n \n let currentDate = NSDate()\n // Here I want to assign the date to the label, but I get an error\n dateLabel.text = currentDate // Cannot assign value of type 'NSDate' to type 'String?'\n }\n}\n```\n\nI understand that NSDate is an object, and UILabel.text expects a string. I tried just using `currentDate.description`, but it outputs something like "2024-01-15 14
No answers yet.
Log in to leave an answer