Hey I am getting this in my code, I'm a new coder and I need help fixing this, here is my code, please tell me my errors! Thank You!
import SwiftUI
enum Emoji: String{
case 😀,😝,🤨,🎉
}
struct ContentView: View {
@State var selection: Emoji = .🎉
var body: some View {
VStack {
Text(selection.rawValue)
.font(.system(size: 150))
Picker("Select Emoji", selection: $selection) {
ForEach(Emoji.allCases, id: \.self){ emoji in
Text(emoji.rawValue)
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Having put your small code snippet into an App playground, the compiler is telling me that your Emoji
struct does not have any member allCases
. Note that the allCases
property is only automatically generated for enumerations conforming to the CaseIterable
protocol. As such, just add , CaseIterable
between the String
raw value type and the open curly brace. See below:
enum Emoji: String, CaseIterable {
case 😀,😝,🤨,🎉
}
Generally, my trick to trying to find these source errors is to comment out certain parts of the code and rebuild and repeat. If commenting out a particular portion of code results in the problem being rectified (not necessarily correct behaviour, but rather just the lack of compilation errors in this part of your code) then I basically drill down and comment out smaller and smaller portions of code until the error becomes more obvious (usually reading a single line of code or so at that point). Occasionally, the compiler provides a specific error that points me in the right direction without having to drill down as far as otherwise but this may not occur depending on where exactly the error is occurring in your code.