Hello,
I am building an app that records a list view of projects in contentView. I am in Xcode 16.2.
I am having trouble when calling contentView in my @main app file and on the ContentView() line shown in the App file shows that ther is a "Missing argument for parameter 'project' in call". It prompts me to fix it with "Insert', project: ,#Project#.>".
However when I do this like the following: ContentView(project: Project) It says "Cannot convert value of type 'Project.Type' to expected argument type 'Project'"
These are my Code below
//Model
import Foundation import SwiftUI
struct Project: Identifiable { var name: String var icon: String var isFavorite: Bool var color: Color let id = UUID() var compoundscore : Float static func CurrentProjects() -> [Project] { return [Project(name: "Test Project", icon: "🍎", isFavorite: true, color: .red, compoundscore: 4.5), Project(name: "Swimming", icon: "🏊", isFavorite: false, color: .orange, compoundscore: 12.5), Project(name: "Archery", icon: "🏹", isFavorite: false, color: .yellow, compoundscore: 37.5) ] }
}
// App view where the error is
import SwiftUI
@main struct MyApp: App { var body: some Scene { WindowGroup { ContentView(project: Project) } } }
// Content View
struct ProjectRow: View { let project: Project
var body: some View {
HStack {
Text(project.icon)
.font(.title)
Text(project.name)
Spacer()
Image(systemName: project.isFavorite ? "heart.fill" : "heart")
}
}
}
struct ContentView: View {
var CurrentProjectlist = Project.CurrentProjects()
var project: Project
var body: some View {
NavigationView {
List {
Section {
if #available(iOS 17.0, *) {
ForEach(CurrentProjectlist) { project in
ProjectRow(project: project)
}
.listRowBackground(
Capsule()
.fill(Color(project.color))
.padding(.vertical, 2).padding(.horizontal, 20)
)
.listRowSeparator(.hidden)
.foregroundColor(.indigo)
} else {
// Fallback on earlier versions
}
} header: {
Text("CI Projects")
}
.listRowBackground(Color.blue)
.foregroundColor(.black)
.listRowInsets(.init(top: 0, leading: 40, bottom: 0, trailing: 40))
.listRowBackground(Color.black)
.listSectionSeparatorTint(.yellow)
.headerProminence(.increased)
.listRowInsets(EdgeInsets.init(top: 0, leading: 50, bottom: 0, trailing: 50))
}
.scrollContentBackground(.hidden)
.background(
Image("cool background")
.resizable()
.scaledToFill()
.clipped()
.edgesIgnoringSafeArea(.all)
.blur(radius: 3)
.overlay(Color.red.opacity(0.2))
)
.environment(\.defaultMinListHeaderHeight, 20)
.environment(\.defaultMinListRowHeight, 70)
.navigationTitle("Navigator")
}
}
}
Welcome to the forum.
The error message is very explicit. You have to pass a Project instance as a parameter, not the structure type itself.
@main struct MyApp: App {
var body: some Scene {
WindowGroup { ContentView(project: Project(name: "dummy", icon: "", isFavorite: true, color: .red, compoundscore: 0)) } // vs project: Project
}
}
Another way is to initialise var project with a dummy instance. No more need to pass parameter in call.
Note: when you post code, please use code formatter to make it readable.
@main struct MyApp: App {
var body: some Scene {
WindowGroup { ContentView() } // no more need for parameter is var is initialised in ContentView
}
}
struct Project: Identifiable {
var name: String
var icon: String
var isFavorite: Bool
var color: Color
let id = UUID()
var compoundscore : Float
static func CurrentProjects() -> [Project] { // name should start with lowercase
return [Project(name: "Test Project", icon: "🍎", isFavorite: true, color: .red, compoundscore: 4.5),
Project(name: "Swimming", icon: "🏊", isFavorite: false, color: .orange, compoundscore: 12.5),
Project(name: "Archery", icon: "🏹", isFavorite: false, color: .yellow, compoundscore: 37.5) ]
}
}
struct ProjectRow: View {
let project: Project
var body: some View {
HStack {
Text(project.icon)
.font(.title)
Text(project.name)
Spacer()
Image(systemName: project.isFavorite ? "heart.fill" : "heart")
}
}
}
struct ContentView: View {
var CurrentProjectlist: [Project] = Project.CurrentProjects()
var project: Project = Project(name: "dummy", icon: "", isFavorite: true, color: .red, compoundscore: 0). // Initial value to avoid having to pass parameter on call
var body: some View {
NavigationView {
List {
Section {
if #available(iOS 17.0, *) {
ForEach(CurrentProjectlist) { project in
ProjectRow(project: project)
}
.listRowBackground(
Capsule()
.fill(Color(project.color))
.padding(.vertical, 2).padding(.horizontal, 20)
)
.listRowSeparator(.hidden)
.foregroundColor(.indigo)
} else {
// Fallback on earlier versions
}
} header: {
Text("CI Projects")
}
.listRowBackground(Color.blue)
.foregroundColor(.black)
.listRowInsets(.init(top: 0, leading: 40, bottom: 0, trailing: 40))
.listRowBackground(Color.black)
.listSectionSeparatorTint(.yellow)
.headerProminence(.increased)
.listRowInsets(EdgeInsets.init(top: 0, leading: 50, bottom: 0, trailing: 50))
}
.scrollContentBackground(.hidden)
.background(
Image("cool background")
.resizable()
.scaledToFill()
.clipped()
.edgesIgnoringSafeArea(.all)
.blur(radius: 3)
.overlay(Color.red.opacity(0.2))
)
.environment(\.defaultMinListHeaderHeight, 20)
.environment(\.defaultMinListRowHeight, 70)
.navigationTitle("Navigator")
}
}
}
I tested and it works OK: