I am trying to use a variable integer throughout multiple methods in my view controller. The secondsLeft variable works fine, but the otherNumber variable won’t work. I get the error: initializer element is not a compile-time constant. Any ideas on how I am supposed to do this? THank you!
@interface ViewController ()
@end
@implementation ViewController
@synthesize countDown,Timerlbl;
int secondsLeft = 500;
int otherNumber =[(AppDelegate *)[UIApplication sharedApplication].delegate otherNumber];
The problem is that you have declared
otherNumberas a global variable and the compiler expects the initial assignment to be a compile-time constant.[delegate otherNumber]results in a selector invocation and this is not a compile-time constant.The solution is to move the assignment into code. For example:
As another note, global variables are generally inadvisable in Objective-C.
@propertyvalues are generally more recommended. Not only that, yourViewControllerclass now has a dependency with yourAppDelegate. Since yourAppDelegatemost likely is the one responsible for instantiating yourViewController, consider having it inject in the value ofotherNumber. For example: