Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在标题中使用@property或@interface_Ios_Objective C_Properties - Fatal编程技术网

Ios 在标题中使用@property或@interface

Ios 在标题中使用@property或@interface,ios,objective-c,properties,Ios,Objective C,Properties,如果要在viewcontroller中使用单个对象,正确的声明方法是什么? 使用@property i my.m文件 @property (nonatomic, strong) UITextView *resolutionText; @property (nonatomic, strong) AWLResolutionView *myView; 或者在我的.h文件中声明它们 @interface { @private UITextView *_resolutionText;

如果要在viewcontroller中使用单个对象,正确的声明方法是什么? 使用@property i my.m文件

@property (nonatomic, strong) UITextView *resolutionText;
@property (nonatomic, strong) AWLResolutionView *myView;
或者在我的.h文件中声明它们

@interface 
{
@private
    UITextView *_resolutionText;
    AWLResolutionView *myView;
}

为了干净的编码,我更喜欢在.m文件中的匿名类别中创建属性

但是,使用@property会自动为您创建一个实例变量,该实例变量的名称与属性的名称相同,前面带有下划线389;,可以从.m文件中访问该实例变量。这叫做合成。 Y 您还可以使用@synthesis将属性手动合成为自定义实例变量


Apple提供了一些清晰的说明,说明了如何在其扩展名中编写干净的代码。

声明私有变量的最佳方法应该是在扩展名为.m的文件中声明为@property。如果您在.m文件中看到,默认情况下有一个名为@interface的扩展类,因此在扩展类中声明了相同的扩展类。也不需要在.h文件中编写额外的代码来声明私有变量


因此,第一种方法是最好的。

第一种方法是在.m文件中声明它。后者是没有匿名类别时的旧版本:还要注意,Objc中没有真正私有的内容。这可能很有用。。谢谢大家的回答!只是补充一下。自动合成有一些不起作用的例外情况。例如,在NSManagedObject子类中。或者重写属性的getter和setter方法。