Lazy initialize implementation in SWIFT (unsolved)

Source: Internet
Author: User

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 .~~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.