Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of properties for primitive types

Tags:

objective-c

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:

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

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.

like image 290
Sean Avatar asked Jan 08 '09 16:01

Sean


People also ask

What is primitive property?

Pure substances have only five fundamental or primitive thermodynamic properties, which are those that cannot be derived from other thermodynamic properties. These are, with their common symbols: T = temperature p = pressure V = volume U = internal energy S = entropy.

What is the purpose of primitive data types?

Primitive types are the most basic data types available within the Java language. There are 8: boolean , byte , char , short , int , long , float and double . These types serve as the building blocks of data manipulation in Java. Such types serve only one purpose — containing pure, simple values of a kind.

What are the characteristics of primitive data types?

Primitive data are only single values; they have no special capabilities. A variable is a name for a location in memory used to store a data value. We use variables to save and restore values or the results of calculations.


1 Answers

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.

like image 197
Marc Charbonneau Avatar answered Sep 29 '22 21:09

Marc Charbonneau