Xeny
1
I need a variable array that can hold 100 things so I have this code.....
import SwiftUI
var mythings = [String](repeating: "a", count: 100)
And I want to add things to my array when the program starts using this type of code.....
mythings.insert("abcde", at: 0)
But the code above doesn't work.
I've tried pasting this code everywhere in Xcode but I keep getting errors.
So I'd look up the errors and try to figure it out, but I still can't figure it out. And I even tried using this viewdidload function.....
var body: some View {
func viewdidload() {
mythings.insert("Item 1", at: 0)
}
And this created an error that I couldn't work out.
Overall nothing's worked!
So if someone could PLEASE help me that would be appreciated.
How do I add things to my array when the app is started?
Thank you.
cukr
2
You can do it in the init of your App struct (not the View)
import SwiftUI
var mythings = [String](repeating: "a", count: 100)
@main
struct FooApp: App {
init() {
mythings.insert("abcde", at: 0)
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
Xeny
3
Wow... I would never have considered putting my code in that other "not the view" section.
But, thanks to you cukr, I have done just that and it's working beautifully. So thanks for replying and, of course, the help :)
1 Like
For future reference, when you have questions about errors in your code, you should post the specific error messages instead of just saying "but I keep getting errors."
Xeny
5
Ok, I'll try to remember that.