Objective C - Declaration Error - please explain
ios, objective-c
Solution
You cannot declare class-level variables in Objective-C classes; instead you need to "hide" them in the implementation file, often giving them `static`-scope so they cannot be accessed externally.
Connections.m:
#import "Connections.h"
static Connections *_sharedInstance = nil;
@implementation Connections
...
@end
And if this is a singleton, you typically define a class-level accessor to create the singleton upon first use:
+ (Connections *)sharedInstance
{
if (_sharedInstance == nil)
{
_sharedInstance = [[Connections alloc] init];
}
return _sharedInstance;
}
(and you'll need to add the declaration in the .h file):
+ (Connections *)sharedInstance;
Problem
``` @interface Connections() { static Connections *this; } @end ``` The above piece of code in .m file throwing compiler error Type name does not allow storage class to be specified at the same time when the static key word is removed it works well - which so obvious. Purpose : I want "Connections" instance static and private. Why is this behavior, please help.