NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

variable in property initializer

Forums > SwiftUI

I fixed the original problem but found a problem with the more general case.

Tried to search for this but had no luck. In my view I have

  • let moves = ["rockj", "Paper", "scissors"]
  • @State private var currentChoic = Int.random(in: 0...moves.count - 1)

Swift does not like this because I can't use moves because it is not initialized yet. I could use a random 1...2 but if I expand this to rock, paper, scissors, lizard, spock I would have to change the count in addition to the moves array (I would probably forget and the app would be wrong). I figure I can use moves.count in the view for the next selection but I hope there is some elegant way to pick a random for the first time. I thought of defining an array of 3 strings, but before I tried I realised when I was putting the values in there would be no error if I just initialized 2 of the strings (I'm not even sure you this has a meaning to Swift). To my thinking moves is a constant so it should be set at compile time and choice is a variable so it should be set at creation and creation is after compile.

Could I move the definition of moves amd make it a static and then ues it in the definition? Ahh that works. Now the question becomes is there a preferred way of doing this in SwiftUI and what is it. Did I find a "good" way of doing this or did I just cram something horrible in my code that works.

Ahh. A little more testing and if I go back to my original code and make moves static I can use it. So, under the covers static is part of the definition of a structure and a simple let gets created during instantiation. Even though it is a constant and can not be changed it still gets initialized when it is not static.

So, this problem is solved but what about if an app started off with a bunch of strings and then there was the possibility of adding to the array. I tried putting moves in another structure as a var, instantinating it in this struct, and using x.moves.count but that failed because x is not built yet. So, how do I do this? I could have the array as a var in this structure, set it equall to a static, use the static to get the count in the initializer, and use the variable to get the count later in the view. That would probably work, but then I have the same question. Is this a good solution or have I crammed some horrible code in that works?

I agonized over where to post this cause it is the result of the 100 days of swift, but, after reading what that forum was for, it did not appear to me that this belonged there. Plus this is a question I can see coming up for other apps. If it does not belong here, please move it.

   

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.