1

SwiftUI View file

struct NoteListView: View {
    @EnvironmentObject var presenter: NoteListPresenter
    var body: some View {
        NavigationView{
            AddNoteButton()
            Text(presenter.noteViewModels[0].title)
        }
    }
}

struct NoteListView_Previews: PreviewProvider {
    static var previews: some View {
        NoteListView()
    }
}

I first got this error when I finished writing the logic and started to writing Views. After add my custom button AddNoteButton() to main view, it gave the following error, although it worked before I added this button. I tried to rewrite my views, but although it works at the beginning at some random point of time it throws this error again(by other words sometimes it works, but when i add some irrelevant code it stops working and discarding changes doesn't solve the problem). It always highlights line, where I use noteViewModels array inside of the NoteListView.

Also I added method which prints array to presenter and it always prints everything.

func printAll(){
    for element in self.noteViewModels{
        print(element.title)
    }
}

I can not add other files to question, because there is too many of them. But i hope that I gave enough information, If you think that there is not enough information you are free to ask me or you can check out github: https://github.com/ShynggysSaparbek/ViperNotesWithRealm/

Xcode version 11.3 Mac OS version Catalina 10.15.2

pkamb
  • 33,281
  • 23
  • 160
  • 191
  • What is the code for `AddNotButton`? What happens if you comment it out? Or comment out parts of it? – Yonat Dec 26 '19 at 19:53
  • Add button is a view. It is actually a navigation view, which shows another views. – Shynggys Saparbek Dec 27 '19 at 05:33
  • I meant to say that the problem code is probably in `AddNoteButton`. To solve it, you can try commenting out parts until it works, and this way find the problematic code. If you're not able to solve it, you can post the code here so others may be able to help. – Yonat Dec 27 '19 at 07:47
  • Code is to big write it here, so i add link to github – Shynggys Saparbek Dec 27 '19 at 10:22
  • The `AddNoteButton` class seems fine, so maybe `noteViewModels[0]` doesn't exist. I.e., `noteViewModels` is empty. Try commenting the line with `Text` and see if the crash goes away. – Yonat Dec 27 '19 at 10:44
  • As I wrote in the post, noteViewModels is not empty, because func printAll() works and prints all the members of array. So noteViewModels only doesn't work as a parameter of ForEach SwiftUI structure. Probably that's a bag of SwiftUI, because previous versions had similar problem with ForEach. So it is not possible to solve it, so I will just rewrite code in with storyboards. Anyway; thanks for help and sorry for grammar mistakes. – Shynggys Saparbek Dec 27 '19 at 13:41
  • What happens if you comment the line with `AddNoteButton()`? What happens if you comment the line with the `Text()`? Try that, and you may be able to pinpoint the source of the problem. – Yonat Dec 27 '19 at 13:43
  • I tried it, and mistakes goes away if comment Text(), but doesn't disappear if comment AddNoteButton. I think, that it is because my array can not be inside of SwiftUI structure for some reason. – Shynggys Saparbek Dec 27 '19 at 13:45
  • Which one? Both? Next step is to comment out the `@EnvironmentObject` as well. Maybe it's not passed from the containing views? – Yonat Dec 27 '19 at 13:46
  • I tried and it doesn't work, I created custom data and saved it in presenter and removed property wrapper, but it still doesn't work. – Shynggys Saparbek Dec 27 '19 at 13:49
  • Just to make sure, try the following code for your view: `struct NoteListView: View { var body: some View { NavigationView{ Text("test") } } }` – Yonat Dec 27 '19 at 13:51
  • It doesn't create error – Shynggys Saparbek Dec 27 '19 at 13:52
  • So now add just one of the deleted lines: `@EnvironmentObject var presenter: NoteListPresenter` . What happens? – Yonat Dec 27 '19 at 13:53

0 Answers0