Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot call value of non-function type 'String?'

I am new to iOS development. I am trying to convert a date value which is in string format(2024-09-22) to 22 Sep 2024.

But I am getting:

Cannot call value of non-function type ‘String?’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

while doing date formatting.

I am getting this error in dateFormatter.dateFormat(from : dateString).

I have tried a below code for the formatting.

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MMM-yyyy"
dateFormatter.timeZone = TimeZone.current
dateFormatter.locale = Locale.current
let convertedDate = dateFormatter.dateFormat(from : dateString)

Could someone help me to solve this issue?

>Solution :

You first need to get Date from given String ("2024-09-22" format: "yyyy-MM-dd"). And create one more DateFormatter to convert Date to new String:

let dateString = "2024-09-22"

let dateFormatterToDate = DateFormatter()
dateFormatterToDate.dateFormat = "yyyy-MM-dd"
let date = dateFormatterToDate.date(from: dateString)

let dateFormatterFromDate = DateFormatter()
dateFormatterFromDate.dateFormat = "dd MMM yyyy"
let result = dateFormatterFromDate.string(from: date!) // 22 Sep 2024
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading