I keep getting this error,” Cannot convert value of type ‘Currentlydoing.Type’ to expected argument type ‘Currrentlydoing”. When I remove the,”done: Currentlydoing” it gives an error saying,”Missing argument for parameter ‘done’ in call insert done: ,#Currenlydoing# “ but when I insert it it gives the original error. What’s the solution?
import SwiftUI
struct ContentView: View {
var done: Currentlydoing
var body: some View {
VStack {
Text(done.sleep ? "Hello":"HI")
}
}
}
struct MainView: View {
var body: some View {
ContentView()
}
}
struct Currentlydoing: Identifiable, Codable {
var id: String
var text : String
var work: Bool
var sleep: Bool
}
The error is because done is not initialised, so it is just a type (Currentlydoing.Type) and not an instance of Currentlydoing.
Essentially, done need to be initialized. You have several ways to solve it.
- 1. Pass argument done at call
in SceneDelegate, where you call ContentView, you probably have code such as:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView() // <<-- You miss done here
If so, change to:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView(done: Currentlydoing(id: "", text: "", work: true, sleep: false)) // Whatever value you want
- 2. Initialise done in ContentView declaration
struct ContentView: View {
var done: Currentlydoing = Currentlydoing(id: "", text: "", work: true, sleep: false)
- 3. Set initial values in struct:
struct Currentlydoing: Identifiable, Codable {
var id: String = ""
var text : String = ""
var work: Bool = true
var sleep: Bool = false
}
and just declare done as:
var done: Currentlydoing = Currentlydoing()