Hello, ¿how are you? I have a question please!. Why if assign String to array app crash?
in line: array?[0] = “horse”
If only use append is all okay.
//
// ContentView.swift
// M2L9Challenger
//
// Created by Orlando J on 22-07-23.
//
import SwiftUI
struct ContentView: View {
@State var array:[String]?
var body: some View {
VStack {
HStack {
Button("Set Nil") {
//Code
setNilButton()
}
.foregroundColor(Color.white)
.padding()
.background(Color.blue)
.cornerRadius(12)
Button("AddString") {
// marks: add string
addStringButton()
}
.foregroundColor(Color.white)
.padding()
.background(Color.blue)
.cornerRadius(12)
}
if array == nil {
Text("Touch second button") } else {
List(array!, id:\.self) { item in
Text(item)
}
}
}
}
func addStringButton() {
array = [String]()
array?[0] = "horse"
array?.append("Ball")
array?.append("Doll")
}
func setNilButton() {
array = nil
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Thanks a lot!!