I'm trying to make a form that will have something to type in (str), a TextField to type it in to and some text below (var = correct) to let me know if I have it correct so far.
For example, (str) = "hello world", if "hel" is in the text field the text below will read "True". The issue is that I can't seem to get correct to use (str) and (field). I've seem similar questions on here that are solved with 'lazy', but that wont work, because ("Property 'field' with a wrapper cannot also be lazy").
func stringSlice(string: String, first: Int, last: Int)->String{
var newStr = ""
for number in 0..<string.count{
if first <= number{
if number <= last{
newStr += "\(string[string.index(string.startIndex, offsetBy: number)])"
}
}
}
return newStr
}
func checkSoFar(answer: String, guess: String)->String{
if stringSlice(string: answer, first: 0, last: guess.count) == guess{
return "True"
}
return "False"
}
struct thisView: View{
var str = "Hello world!"
@State private var field = ""
@State private var correct = checkSoFar(answer: str, guess: field)
var body: some View{
Form{
Text(self.str)
TextField("Type the above", text: $field){
}
Text(correct)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
thisView()
}
}
Any ideas would be greatly appreciated. Thank you.