Objective C - Static and global variable?

前端 未结 2 494
清歌不尽
清歌不尽 2021-01-12 02:01

In my .m file for a class named Ad , I have 3 static strings

static NSString *AdStateDisabled = @\"disable\";
static NSString *AdStateExpired = @\"expired\";         


        
2条回答
  •  攒了一身酷
    2021-01-12 02:42

    You could add the following declarations to your HomeViewController.h header, which would then need to be imported anywhere you wanted access to the strings.

    //HomeViewController.h
    extern NSString *AdStateDisabled;
    extern NSString *AdStateExpired;
    extern NSString *AdStateActive;
    

    Then alter your definitions to remove 'static'.

    //HomeViewController.m
    NSString *AdStateDisabled = @"disable";
    NSString *AdStateExpired = @"expired";
    NSString *AdStateActive = @"active";
    

    If you don't want a user of the strings to have to import HomeViewController.h then you could also just define those strings in AdState.h and put the definitions into AdState.m (and remove them from HomeViewController.m) after which users of the string would just need to import AdState.h to use the strings.

提交回复
热议问题