search

Home  >  Q&A  >  body text

Objective-C 中下列类属性的写法有何不同?

1. 在 .h 文件中,例如:

@interface PullToRefreshView : UIView {
	UILabel *lastUpdatedLabel;
	UILabel *statusLabel;
	CALayer *arrowImage;
	UIActivityIndicatorView *activityView;
}

2. 在 .h 文件中,例如:

@property (nonatomic, strong) UIScrollView *scrollView;

3. 在 .m 文件中,例如:

@interface PullToRefreshView

@property (nonatomic, strong) NSArray *titles;

@end

4. 在 .m 文件中,例如:

@implementation TopicListViewController {
    PullToRefreshView *pull
}

简而言之,是否是
1. 在头文件中的是 public 属性,在 .m 中的是 private 属性?
2. 在 @interface 和 @implementation 中用大括号扩起来的和 @property 有何区别?(除了synthesize方面)
3. 一般情况下应该如何写?

PHPzPHPz2769 days ago411

reply all(4)I'll reply

  • 迷茫

    迷茫2017-04-21 11:18:48

    Assuming that the special way of using @private to declare variables is not considered, in object-c, as long as the variables and attributes are declared, they can be used in the .m file that contains this declaration.

    Making everything public is certainly not in line with the encapsulation in object-oriented design. Those variables and attributes written directly in .m are to hide information and ensure encapsulation. The official name of this method is Category, which is The necessary techniques for writing object-c lib.

    So, the answer to all your questions:

    1. They are all public, except that .m files cannot be #imported, thus indirectly implementing private
    2. The instance variable enclosed in curly brackets is just a simple numerical value. It cannot be bound to the get/set method, and cannot automatically retain/copy/atomic. It is equivalent to a simple local variable that follows the instance; the property is It is a syntactic encapsulation (so-called syntax sugar) of the get/set method, turning two method declarations into one property declaration, and helping you complete many basic tasks by annotating various attributes.
    3. I have seen some libs write the declarations that are exposed to the outside in Foo.h, and then write the declarations that are not exposed to the outside in Foo+Internal.h. Through this convention, I tell the caller not to use my internal declarations. Variables, properties and methods. Personally, I prefer this approach.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-21 11:18:48

    Things written directly inside {} are instance variables, and those written with @property in front are properties. For attributes, obj-c will automatically generate an instance variable, and then when accessing the attribute (using the get/set method or using obj.attr), this automatically generated instance variable will be used and do some other additional things.

    reply
    0
  • 迷茫

    迷茫2017-04-21 11:18:48

    @Huan Du has already said it very well, let’s talk about my usage habits

    1. Instead of using ivar, declare @property in Class Extension, so that you can use self.xxx internally to get/set value, and the code will look more comfortable.

    2. In the header file, only declare methods/properties that are visible to the outside world, so that the content of the header file is as small and clear as possible. If there is a property that is read-only externally but read-writeable internally, the property can be re-declared as readwrite in Class Extension.

    reply
    0
  • 高洛峰

    高洛峰2017-04-21 11:18:48

    1. instance, public
    2. property (instance, setter, getter automatically generated), public
    3. Same as 2
    4. instance, public

    You can add @private in front of instance

    My general approach, which is also the code format automatically generated by XCode, is to write public as @property, put it in the .h file, and write private as

    @interface Foo ()
    
    @property Bar bar;
    
    @end

    Put it in the .m file.

    The empty parentheses are category. It doesn’t matter if you don’t write it or write it as hidden. In short, other classes will not be able to see this property.

    reply
    0
  • Cancelreply