Making some code only run once

后端 未结 4 818
半阙折子戏
半阙折子戏 2020-12-02 13:41

I have some code that I would like to run only once in my MainViewController. It should run every time the user starts the app, but only after the MainViewController has loa

4条回答
  •  失恋的感觉
    2020-12-02 13:57

    I don't see any problem with that code. I like using a BOOL (as you did) and then assigning either YES/NO or TRUE/FALSE just so that the code reads more nicely. I would assign TRUE to firstRun in didFinishLaunching, and set it FALSE after the code executes. In my code these type of conditionals usually look like this:

    @synthesize firstRun;
    
    -(void)viewDidLoad {
       [super viewDidLoad];
        if (firstRun) {
            // code to run only once goes here
            firstRun = FALSE;
        }
    }
    

提交回复
热议问题