Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Using @private to hide properties from being set

I am wring a class that has a bunch of properties that I want to only use internally. Meaning I don't want to be able to have a user access them when they have created my class. Here is what I have in my .h but it still doesn't hide those from the autocomplete menu (hitting escape to see list) in XCode:

@interface Lines : UIView {
    UIColor *lineColor;
    CGFloat lineWidth;

    @private
        NSMutableArray *data;
        NSMutableArray *computedData;
        CGRect originalFrame;
        UIBezierPath *linePath;
        float point;
        float xCount;
}


@property (nonatomic, retain) UIColor *lineColor;
@property (nonatomic) CGFloat lineWidth;
@property (nonatomic) CGRect originalFrame;
@property (nonatomic, retain) UIBezierPath *linePath;
@property (nonatomic) float point;
@property (nonatomic) float xCount;
@property (nonatomic, retain) NSMutableArray *data;
@property (nonatomic, retain) NSMutableArray *computedData;

I thought that using @private was what I needed, but maybe I have done it wrong. Does something need to also be done in my .m?

like image 466
Nic Hubbard Avatar asked Oct 26 '11 22:10

Nic Hubbard


1 Answers

@private only affects ivars. It doesn't affect properties. If you want to avoid making properties public, put them in a class extension instead. In your .m file, at the top, put something like

@interface Lines ()
@property (nonatomic) CGRect originalFrame;
// etc.
@end

This looks like a category, except the category "name" is blank. It's called a class extension, and the compiler treats it as if it were part of the original @interface block. But since it's in the .m file, it's only visible to code in that .m file, and code outside it can only see the public properties that are declared in the header.

like image 189
Lily Ballard Avatar answered Oct 07 '22 00:10

Lily Ballard