目标C:我应该分配变量并创建属性,还是仅其中之一就足够了? [英] Objective C: Should I assign the variable AND create a property or is just one of them enough?

查看:52
本文介绍了目标C:我应该分配变量并创建属性,还是仅其中之一就足够了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个头文件(.h),我想声明name,但是我认为所有这些方式都是一样的,因为我没有看到功能上的任何区别.你能告诉我两者之间有什么区别吗?

I have got a header file (.h) and I want to declare name but all these ways work the same I think because I haven't seen any difference with functionality. Could you tell me what the difference is between:

这带有两个声明:

@interface someClass : UIViewController
{
    NSString *name;
}

@property (nonatomic, copy) NSString *name;

@end

无变量:

@interface someClass : UIViewController
{

}

@property (nonatomic, copy) NSString *name;

@end

或者没有属性:

@interface someClass : UIViewController
{
    NSString *name;
}

@end

推荐答案

   @interface someClass : UIViewController
   {
     NSString *name;
   }

   @property (nonatomic, copy) NSString *name;

   @end

这样做,您将显式声明一个属性和一个ivar.

Doing this you will explicitly declare both a property and an ivar.

属性只是一组方法:

 - (void)setName:(NSString*)name;
 - (NSString*)name;

ivar是保存属性方法管理的值的内存存储.这使您可以执行以下操作:

An ivar is the memory store holding the value that the property methods manage. This allows you to do:

self.name = ... // access through setter method
name = ... // direct access

使用属性的优点是它们可以为您处理内存管理.例如,在您的情况下,该属性的类型为copy:这意味着使用第一种语法(self.name = ...)将完成对象的副本.如果不使用属性,则需要明确地执行以下操作:name = [originalString copy];以获得相同的效果.

The advantage of using properties is that they deal with memory management for you. E.g., in your case, the property is of type copy: this means that with the first syntax (self.name = ...) a copy of the object will be done. If not using properties, you would explicitly need to do: name = [originalString copy]; to obtain the same effect.

您可以为属性(但不是ivars)指定的其他选项包括:strongweak所有权.

Other options you can specify for properties (but not ivars) are: strong and weak ownerships.

此外,属性还表示一个公共接口,可从类外部访问变量.

Furthermore, a property also represents a public interface to access the variable from outside your class.

使用直接访问权限,您可以自行进行内存管理(如果不使用ARC). 如果您使用的是ARC并且未定义属性,则将无法通过指定所有权(强,弱,保留)来控制如何管理内存.

Using direct access you are on your own as to memory management (if you are not using ARC). If you are using ARC and don't define properties, you will not be able to control how the memory is managed by specifying the ownership: strong, weak, retain).

 @interface someClass : UIViewController
 {

 }

 @property (nonatomic, copy) NSString *name;

 @end

这里您仅声明属性; ivar是推断"的,通过实现文件中的@synthesize指令.只有在Objective C 2.0和更高版本中才有可能(以前,上面的ivar声明是强制性的).

Here you only declare the properties; the ivar is "inferred" by the @synthesize directive in your implementation file. This is only possible in Objective C 2.0 and later (previously, the ivar declaration as above was mandatory).

与上述相同的注意事项也有一些细微差别:使用较旧版本的LLVM(ObjC编译器),您将无法直接引用自动合成的ivar.在当前版本的LLVM中,如果省略@synthesize指令,则还将声明一个以您的属性命名的自动ivar(在您的情况下为_name).

The same considerations as above applies, with a minor nuance: with older versions of LLVM (ObjC compiler) you will not be able to reference directly the auto-synthesized ivar; with current version of LLVM, if you omit the @synthesize directive, then an automatic ivar named after your property would also be declared (in your case it would be _name).

最后一段可能看起来有些高级"或人为设计,但您可以放心地忽略它.

This last paragraph may seem a bit "advanced", or contrived, but you can safely ignore it.

 @interface someClass : UIViewController
 {
    NSString *name;
 }

 @end

在这种情况下,您仅声明了ivar.没有访问器方法.您将需要自行处理内存管理(如果不使用ARC),此外,您将无法从类外部访问变量.为此,您需要访问器.

In this case you are only declaring the ivar. No accessor methods. You will need to handle memory management on your own (if not using ARC), futhermore you will not be able to access the variable from outside the class. For that you need accessors.

希望这会有所帮助.

这篇关于目标C:我应该分配变量并创建属性,还是仅其中之一就足够了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆