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. 一般情况下应该如何写?
迷茫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:
天蓬老师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.
迷茫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.
高洛峰2017-04-21 11:18:48
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.