error: expected identifier or '(' before 'public' - objective C
I a开发者_如何学编程m getting this error when i declare my instance variable as
@interface FOO : NSObject
{
@public
int a;
}
@public
-(id)init;
-(void)dealloc;
@end
error: expected identifier or '(' before 'public'
You do not use @public
or other access keywords outside the interface definition, as you don't declare public or private methods in Objective-C.
As long as you expose a method in a header/interface it's automatically publicly accessible from outside the class. If you only add an implementation, but don't expose it in the header/interface (or only expose it in a class extension), it's private.
@interface FOO : NSObject
{
@public
int a;
}
- (id)init;
- (void)dealloc;
@end
Now why you would want to declare a public int a
instance variable (instead of using a property) or explicitly declare two methods that NSObject
already has is beyond me.
In objective-c methods block cannot contain @public (or @private) block - it is aplicable only to instance variable and all methods in objective-c classes are public, so to fix your error remove 2nd @public in your code:
@interface FOO : NSObject
{
@public
int a;
}
-(id)init;
-(void)dealloc;
@end
精彩评论