In oC, we use
-(Cardmatchinggame *) Game {If (! _ Game) _ game = [[cardmatchinggame alloc] initwithcardcount: [self. cardbuttons count] usingdeck: [self createdeck]; return _ game ;}
Method To implement lazy initialize, that is, the object is instantiated only when it is called for the first time. According to the above method, I tried to implement it using the following code in swift:
VaR gamemodel: cardmatchinggame? {If! Self. gamemodel {return cardmatchinggame (cardcount: cardbuttons. Count, usingdeck: playdec)} else {return self. gamemodel }}
There is no problem in compilation, but it will enter an endless loop during running. On Self. gamemodel, The get method of gamemodel will be called again, repeating until the program crashes.
Use the lazy keyword, ....
The variable cannot be found.
If the lazy keyword is removed or this error occurs, I think it should not be the lazy problem. It must be a matter of negligence in the declaration process and cannot be solved yet.
Now we can use another method to replace this and continue. Wait until we find the solution or the error .~~~