Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the default attributes for Objective-C properties?

What are the default attributes for a properpty when you do not list any in objective C?

Such as for example if I wrote this:

@property float value; 

What would the defaults be, like is it read only, does it retain...etc.?

like image 788
rs14smith Avatar asked Oct 19 '11 19:10

rs14smith


People also ask

What are Objective C properties?

The goal of the @property directive is to configure how an object can be exposed. If you intend to use a variable inside the class and do not need to expose it to outside classes, then you do not need to define a property for it. Properties are basically the accessor methods.

What is the default for synthesized properties?

By default if you do not specify a name for the instance variable the @synthesize statement will use the property name (without the leading underscore).

What is the strong attribute of property?

strong / retain : Declaring strong means that you want to “own” the object you are referencing. Any data that you assign to this property will not be destroyed as long as you or any other object points to it with a strong reference.

What is strong and Nonatomic in Objective C?

nonatomic property means @synthesize d methods are not going to be generated threadsafe -- but this is much faster than the atomic property since extra checks are eliminated. strong is used with ARC and it basically helps you , by not having to worry about the retain count of an object.


1 Answers

The default/implicit values are atomic, readwrite, and assign.

atomic

This means that the value is read/written atomically. Contrary to the somewhat popular misconception, atomicity does not equate to thread safety. In simple terms, it guarantees that the value you read or write will be read or written in whole (when the accessors are used). Even when you use accessors all the time, it's not strictly thread safe.

readwrite

The property is given a setter and a getter.

assign

This default is usually seen used for POD (Plain-Old-Data) and builtin types (e.g. int).

For NSObject types, you will favor holding a strong reference. In the majority of cases, you will declare the property copy, strong, or retain. assign performs no reference count operations. See also: http://clang.llvm.org/docs/AutomaticReferenceCounting.html#property-declarations

strong

The property may be implicitly strong under ARC in some cases:

A property of retainable object pointer type which is synthesized without a source of ownership has the ownership of its associated instance variable, if it already exists; otherwise, [beginning Apple 3.1, LLVM 3.1] its ownership is implicitly strong. Prior to this revision, it was ill-formed to synthesize such a property.

like image 193
justin Avatar answered Oct 21 '22 02:10

justin