UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Help with error: "No exact matches in call to initializer"

Forums > SwiftUI

Hello, wise and kind people! Mostly total Swift/SwiftUI noob here (have some python experience - very different!), though I've worked through Paul's SwiftUI iDine tutorial. Wanting to level up my skills through practice, and build myself a simple practice app, but can't seem to solve the above, fairly opaque, error. I'm trying to build a simple calendar date object overlaying a date on a photo, and just set (for the moment) the date overlay to today's date. Styling not done yet, obvi. Here's my code, in the SwiftUI view object. What am I doing wrong? Have tried various versions of return types, various places declaring the function, and trying various unwrapping/coalescing options at the various declarations/assignments and can't seem to find anything that works. Thank you!

struct CalendarDay: View {

    var body: some View {
        ZStack() {
            Image(systemName: "photo")
                .resizable()
                .aspectRatio(contentMode: .fit)
                .background(Color.gray)
                .foregroundColor(Color.black)
                .cornerRadius(40)
            Text(getTodayDate())
                .font(.headline)
            }
        }
        func getTodayDate() -> Any?
        {
            let now = Date()
            let calendar = Calendar.current
            let components = calendar.dateComponents([.day], from: now)
            return components.day

        }
    }

struct CalendarDay_Previews: PreviewProvider {
    static var previews: some View {
        CalendarDay()
    }
}

2      

Text can't take an Any? as a parameter. You need to convert components.day into a String and change the return type of getTodayDate() to String.

Something like this will work:

return components.day != nil ? String(components.day!) : ""

5      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.