ошибка, мешающая мне построить свой проект, который неоднозначен - PullRequest
0 голосов
/ 11 марта 2020

Я получаю эту ошибку в моем файле SwiftUI. Я пробовал компилировать в Swift 5.3 и 5.4 beta, но я продолжаю получать ту же ошибку. Этот файл содержит пользовательские представления, созданные на основе созданной мной внутренней структуры. Этот фреймворк отлично работает на других проектах, поэтому я устранил эту проблему. У меня есть много операторов if, создающих представления, основанные на выборе пользователя. Код работал раньше, но он случайно начал указывать эту ошибку.

Image of error

 import SwiftUI
 struct Cheatsheet: View {
 var Letters =         ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","Number 1","Number 2","Number 3","Number 4","Number 5","Number 6","Number 7","Number 8","Number 9","Number 0","Number Sign"]
@State private var selection = 0
var body: some View {
    VStack {
        //Logic
        if selection == 0 {
            NavigationView {
                NavigationLink(destination: LetterA()) { LetterA()
                }
            }
        }
        if selection == 1 {
            NavigationView {
                NavigationLink(destination: LetterB()) { LetterB()
                }
            }
        }
        if selection == 2 {
            NavigationView {
                NavigationLink(destination: LetterC()) { LetterC()
                }
            }
        }
        if selection == 3 {
            NavigationView {
                NavigationLink(destination: LetterD()) { LetterD()
                }
            }
        }
        if selection == 4 {
            NavigationView {
                NavigationLink(destination: LetterE()) { LetterE()
                }
            }
        }
        if selection == 5 {
            NavigationView {
                NavigationLink(destination: LetterF()) { LetterF()
                }
            }
        }
        if selection == 6 {
            NavigationView {
                NavigationLink(destination: LetterG()) { LetterG()
                }
            }
        }
        if selection == 7 {
            NavigationView {
                NavigationLink(destination: LetterH()) { LetterH()
                }
            }
        }
        if selection == 8 {
            NavigationView {
                NavigationLink(destination: LetterI()) { LetterI()
                }
            }
        }
        if selection == 9 {
            NavigationView {
                NavigationLink(destination: LetterJ()) { LetterJ()
                }
            }
        }
        if selection == 10 {
            NavigationView {
                NavigationLink(destination: LetterK()) { LetterK()
                }
            }
        }
        if selection == 11 {
            NavigationView {
                NavigationLink(destination: LetterL()) { LetterL()
                }
            }
        }
        if selection == 12 {
            NavigationView {
                NavigationLink(destination: LetterM()) { LetterM()
                }
            }
        }
        if selection == 13 {
            NavigationView {
                NavigationLink(destination: LetterN()) { LetterN()
                }
            }
        }
        if selection == 14 {
            NavigationView {
                NavigationLink(destination: LetterO()) { LetterO()
                }
            }
        }
        if selection == 15 {
            NavigationView {
                NavigationLink(destination: LetterP()) { LetterP()
                }
            }
        }
        if selection == 16 {
            NavigationView {
                NavigationLink(destination: LetterQ()) { LetterQ()
                }
            }
        }
        if selection == 17 {
            NavigationView {
                NavigationLink(destination: LetterR()) { LetterR()
                }
            }
        }
        if selection == 18 {
            NavigationView {
                NavigationLink(destination: LetterS()) { LetterS()
                }
            }
        }
        if selection == 19 {
            NavigationView {
                NavigationLink(destination: LetterT()) { LetterT()
                }
            }
        }
        if selection == 20 {
            NavigationView {
                NavigationLink(destination: LetterU()) { LetterU()
                }
            }
        }
        if selection == 21 {
            NavigationView {
                NavigationLink(destination: LetterV()) { LetterV()
                }
            }
        }
        if selection == 22 {
            NavigationView {
                NavigationLink(destination: LetterW()) { LetterW()
                }
            }
        }
        if selection == 23 {
            NavigationView {
                NavigationLink(destination: LetterX()) { LetterX()
                }
            }
        }
        if selection == 24 {
            NavigationView {
                NavigationLink(destination: LetterY()) { LetterY()
                }
            }
        }
        if selection == 25 {
            NavigationView {
                NavigationLink(destination: LetterZ()) { LetterZ()
                }
            }
        }
        //END -> Logic
        Picker(selection: $selection, label: Text("")) {
            ForEach(0 ..< Letters.count) {
                Text(Letters[$0])
            }
        }
    }
}
}

 struct Cheatsheet_Previews: PreviewProvider {
static var previews: some View {
    Cheatsheet()
}
}

Ответы [ 2 ]

0 голосов
/ 11 марта 2020

Проблема здесь

Picker(selection: $selection, label: Text("")) {
            ForEach(0 ..< Letters.count) {
                Text(Letters[$0])
            }
        }

вам нужно self с Letters, поэтому должно быть

Picker(selection: $selection, label: Text("")) {
            ForEach(0 ..< Letters.count) {
                Text(self.Letters[$0])
            }
        }

И вторая проблема - количество просмотров, добавленных в 1 группу, Вы не можете иметь более 10 просмотров на 1 группу. Вам было бы лучше просто сделать функцию, которая заботится о переключении. и его очиститель.

Вот пример

struct Test: View {
     var Letters =         ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","Number 1","Number 2","Number 3","Number 4","Number 5","Number 6","Number 7","Number 8","Number 9","Number 0","Number Sign"]
    @State private var selection = 0

    var body: some View {
         VStack {
            // Logic
            getLetterView(selection: self.selection)

               //END -> Logic
               Picker(selection: $selection, label: Text("")) {
                   ForEach(0 ..< Letters.count) {
                    Text(self.Letters[$0])
                   }
               }
           }
    }
}

func getLetterView(selection: Int) -> AnyView? {
    var destination: AnyView? = nil
    if selection == 0 {
        destination = AnyView(LetterA())
    } else if(selection == 1) {
        destination = AnyView(LetterB())
    } else if(selection == 2) {
        destination = AnyView(LetterC())
    } else if(selection == 3) {
        destination = AnyView(LetterD())
    } else if(selection == 4) {
        destination = AnyView(LetterE())
    } else if(selection == 5) {
        destination = AnyView(LetterF())
    } else if(selection == 6) {
        destination = AnyView(LetterG())
    } else if(selection == 7) {
        destination = AnyView(LetterH())
    } else if(selection == 8) {
        destination = AnyView(LetterI())
    } else if(selection == 9) {
        destination = AnyView(LetterJ())
    }

// Rest of your if conditions

    return  AnyView(NavigationView {
               NavigationLink(destination: destination) {
                destination
               }
           })
}
0 голосов
/ 11 марта 2020

Можно иметь не более 10 представлений в одном контейнере, поэтому сгруппируйте их по 10, например

Group {
        if selection == 0 {
            NavigationView {
                NavigationLink(destination: LetterA()) { LetterA()
                }
            }
        }
        if selection == 1 {
            NavigationView {
                NavigationLink(destination: LetterB()) { LetterB()
                }
            }
        }
        ...
        if selection == 9 {
            NavigationView {
                NavigationLink(destination: LetterJ()) { LetterJ()
                }
            }
        }
}
Group {
        if selection == 10 {
            NavigationView {
                NavigationLink(destination: LetterK()) { LetterK()
                }
            }
        }
        ...
}

или, лучше, переосмыслите дизайн для использования ForEach View Builder.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...