亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Objective-C 中下列類屬性的寫法有何不同?
PHPz
PHPz 2017-04-21 11:16:48
0
4
465

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
}

簡(jiǎn)而言之,是否是
1. 在頭文件中的是 public 屬性,在 .m 中的是 private 屬性?
2. 在 @interface 和 @implementation 中用大括號(hào)擴(kuò)起來(lái)的和 @property 有何區(qū)別?(除了synthesize方面)
3. 一般情況下應(yīng)該如何寫?

PHPz
PHPz

學(xué)習(xí)是最好的投資!

reply all(4)
迷茫

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.
Peter_Zhu

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.

迷茫

@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.

小葫蘆
  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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template