对基本类型使用属性

时间:2022-04-17 12:57:39

I'm having trouble understanding when to use properties in Objective C 2.0. It seems you do not need a property for a primitive type such as: int, bool, float. Is this true? I have seen examples showing properties for these types and other leaving them out. For example, in Apple's sample code they have:

我不知道什么时候在Objective c2.0中使用属性。看起来您不需要原始类型的属性,比如:int、bool、float。这是真的吗?我已经看到了一些例子,它们显示了这些类型的属性,而其他的则没有显示出来。例如,在苹果的样本代码中,他们有:

...
@interface Book : NSObject {
    // Primary key in the database.
    NSInteger primaryKey;
    // Attributes.
    NSString *title;
    NSDate *copyright;
    NSString *author;

    BOOL hydrated;
    BOOL dirty;
    NSData *data;
}

@property (assign, nonatomic, readonly) NSInteger primaryKey;
// The remaining attributes are copied rather than retained because they are value objects.
@property (copy, nonatomic) NSString *title;
@property (copy, nonatomic) NSDate *copyright;
@property (copy, nonatomic) NSString *author;
...

Apple SQLite Book List Sample Code

苹果SQLite图书列表示例代码

So as you can see they don't use a property for BOOL, but they treat it has an instance variable throughout the implementation file, reading the value and setting the value. Searching online I have found tutorials that do use properties for these types such as: (@property BOOL flag). Can someone shed some light on this topic for me? Thanks.

如您所见,他们不使用BOOL的属性,但是他们在实现文件中使用实例变量,读取值并设置值。在网上搜索时,我找到了一些使用属性的教程,比如:(@property BOOL flag)。有人能给我讲讲这个话题吗?谢谢。

1 个解决方案

#1


34  

Yes, you should declare a property for primitive types. The only real difference is you should use assign (which is the default, so you can also leave it out) instead of copy or retain. I can't speak for the rest of the example, but it's probably accessing the internal instance variable directly, or if it's being accessed from another class key value coding is generating an accessor (which is really bad form). I'm guessing it's the former; if I don't need a special accessor and the instance variable isn't used outside the class I'll just refer to it directly rather than declaring a property. Some people might argue against that I suppose, but it seems a little excessive to me.

是的,您应该为原始类型声明属性。唯一的区别是您应该使用assign(这是默认值,所以您也可以省略它)而不是copy或retain。我不能代表这个示例的其余部分,但它可能直接访问内部实例变量,或者如果从另一个类键值编码访问它,则生成一个访问器(这是非常糟糕的形式)。我猜是前者;如果我不需要特殊的访问器,并且实例变量不在类之外使用,我将直接引用它,而不是声明属性。有些人可能会反对我的观点,但对我来说有点过分。

#1


34  

Yes, you should declare a property for primitive types. The only real difference is you should use assign (which is the default, so you can also leave it out) instead of copy or retain. I can't speak for the rest of the example, but it's probably accessing the internal instance variable directly, or if it's being accessed from another class key value coding is generating an accessor (which is really bad form). I'm guessing it's the former; if I don't need a special accessor and the instance variable isn't used outside the class I'll just refer to it directly rather than declaring a property. Some people might argue against that I suppose, but it seems a little excessive to me.

是的,您应该为原始类型声明属性。唯一的区别是您应该使用assign(这是默认值,所以您也可以省略它)而不是copy或retain。我不能代表这个示例的其余部分,但它可能直接访问内部实例变量,或者如果从另一个类键值编码访问它,则生成一个访问器(这是非常糟糕的形式)。我猜是前者;如果我不需要特殊的访问器,并且实例变量不在类之外使用,我将直接引用它,而不是声明属性。有些人可能会反对我的观点,但对我来说有点过分。