2

Glad to post my first question here!

I've been playing around with SwiftUI for a few weeks now and during a bigger project, I found the following bug.

If you have a TabView and a list inside it, if you try to change the tab while the scroll animation takes place, the app will crash with FATAL ERROR: "Thread 1: signal SIGABRT".

Console:

  • BugTest[11830:362796] precondition failure: attribute failed to set an initial value: 98

Have you ever encountered this? Is there any way I can solve this issue without changing my list into a ForEach?

Thank you in advance!

Code:

import SwiftUI

struct ContentView: View {
    var body: some View {
                TabView {
                    list()
                        .tabItem {
                            Image(systemName: "doc")
                                .font(.system(size: 25))
                    }

                    Text("Testing the bug")
                        .tabItem {
                            Image(systemName: "list.dash")
                                .font(.system(size: 25))
                    }
        }
    }
}

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

struct list: View {

    var body: some View {
        List(0..<50){_ in
            Text("test")
        }
    }
}
Diego Bascans
  • 1,083
  • 1
  • 6
  • 14

2 Answers2

-2

According to this post, the error occurred because the items in the list were not conforming to the Identifiable protocol.

struct ContentView: View {
    var body: some View {
        TabView {
            list()
                .tabItem {
                    Image(systemName: "doc")
                        .font(.system(size: 25))
            }

            Text("Testing the bug")
                .tabItem {
                    Image(systemName: "list.dash")
                        .font(.system(size: 25))
            }
        }
    }
}

struct list: View {
    var elements: [CustomInt] = []

    init() {
        for i in 0...1000{
            elements.append(CustomInt(text:String(i)))
        }
    }

    var body: some View {
        List(elements){element in
            Text(element.text)
        }
    }
}

struct CustomInt: Identifiable{
    var id = UUID()
    var text:String
}

simibac
  • 7,672
  • 3
  • 36
  • 48
-3

This should work

struct ContentView: View {
var body: some View {
            TabView {
               VStack{
                list()
                    .tabItem {
                        Image(systemName: "doc")
                            .font(.system(size: 25))
                }

                Text("Testing the bug")
                    .tabItem {
                        Image(systemName: "list.dash")
                            .font(.system(size: 25))
                }
    }
}
}}

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

struct list: View {
var body: some View {
    List(0..<50){_ in
        Text("test")
    }
}}
Mohamed Wasiq
  • 490
  • 4
  • 17
  • Thank you very much! I slightly modified your version and it works in my case – Catalin Olaru Dec 20 '19 at 00:18
  • Could you please explain the difference? Why that works? Looks like you simply removed his code to call list() – norekhov Jan 21 '20 at 14:48
  • 1
    I see - you tried to put it in VStack. For me it doesn't help. Replacing with ForEach also doesn't help. – norekhov Jan 21 '20 at 15:02
  • Don’t know but it weirdly works in my case. SwiftUI has lot of bugs with List. Could you try with Scroll View ? Or post the code where it doesn’t work for you. – Mohamed Wasiq Jan 21 '20 at 18:17
  • I edited the code a bit but looks like could you please check? Looks like putting all tabs inside VStack is not what OP wanted. In this edition TabView won't show tabs at all I think. – norekhov Jan 21 '20 at 19:40