NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: Day 47 - Habit Tracker - Compiler errors when creating detail view

Forums > 100 Days of SwiftUI

Hi all,

I am getting goin on building the habit tracker app and I am running into snafu's right out of the gate.

I am getting the error "'ModifyHabitView' initializer is inaccessible due to 'private' protection level" in both files pasted below. I'm attaching images, but in ContentView it's on the line with ModifyHabitView(habits: self.habits). And in ModifyHabitView it's down in the preview struct. I'm attaching screen shots and a link to the repo as well.

Can someone tell me why this error is occurring?

Repo

ContentView

//
//  ContentView.swift
//  habit-tracking
//
//  Created by Travis Brigman on 2/10/21.
//  Copyright © 2021 Travis Brigman. All rights reserved.
//

import SwiftUI

struct ContentView: View {
    @ObservedObject var habits = Activities()

    @State private var showModifyHabitView = false

    var body: some View {
                NavigationView {
            List {
                ForEach(habits.activities) {
                    habit in HStack {
                        VStack {
                            Text(habit.activityTitle)
                                .font(.headline)
                            Text(habit.activityDescription)
                        }
                        Spacer()
                        Text("\(habit.activityCompleted)")

                    }
                }
                .onDelete(perform: removeItems)
            }
            .navigationBarTitle("Habit Tracker")
            .navigationBarItems(leading: EditButton() ,trailing: Button( action: { self.showModifyHabitView = true
            }){
                Image(systemName: "plus")
            })
        }
        .sheet(isPresented: $showModifyHabitView) {
            ModifyHabitView(habits: self.habits)
        }
    }

    func removeItems(at offsets: IndexSet) {
        habits.activities.remove(atOffsets: offsets)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

ContentView

ModifyHabitView

//
//  ModifyHabitView.swift
//  habit-tracking
//
//  Created by Travis Brigman on 2/10/21.
//  Copyright © 2021 Travis Brigman. All rights reserved.
//

import SwiftUI

struct ModifyHabitView: View {
    @Environment(\.presentationMode) var presentationMode
    @ObservedObject var habits: Activities
    @State private var title: String
    @State private var description: String

    var body: some View {

        NavigationView {
            Form {
                TextField("Title", text: $title)
                TextField("Description", text: $description)
            }
        }
        .navigationBarTitle("Add New Habit")
        .navigationBarItems(trailing: Button("Save"){

            let habit = SingleActivity(activityTitle: self.title, activityDescription: self.description, activityCompleted: 0)
                self.habits.activities.append(habit)
                self.presentationMode.wrappedValue.dismiss()

        })
    }
}

struct ModifyHabitView_Previews: PreviewProvider {
    static var previews: some View {
        ModifyHabitView(habits: Activities())
    }
}

ModifyHabitView

1      

Hi,

It might not help at all but if you change the line in Activities class from @Published var activities =[SingleActivity]() to @Published var activities: [SingleActivity] ?

1      

Hi @Tiberiu27,

Thanks for looking at my problem and providing a suggestion. I tried your suggestion and the compiler immediately showed an error on that line asking me to change it back to @Published var activities =[SingleActivity]()

Let me know if you have any other ideas.

1      

Hi,

So, I downloaded the code and I fooled around for a bit and then I saw it.

In the ModifyHabitView changed

  @State private var title: String
  @State private var description: String

to this:

  @State private var title = ""
  @State private var description = ""

It seems it works for now :)

1      

That was it!

I wonder why the other way doesnt work...🤔

1      

Not quite sure, let me know when you find out :D

1      

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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.