What are the default attributes for Objective-C properties?
Asked Answered
H

3

49

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

Hugmetight answered 19/10, 2011 at 19:49 Comment(4)
Please read the FAQ - You haven't accepted any answers. At least one answer I see solved your problem. stackoverflow.com/faqFlowerage
A search for "properties" in the Apple docs takes you straight to the specification for "Declared Properties" which has a section "Property Declaration Attributes" that completely answers your question.Beestings
@JoshCaswell, thank link is now dead and this question is now one of the first Google hits. So it's more historical constructive to just ignore the question or answer it with references to an external site.Maldonado
@JamesMcMahon: The information can still be found in the Apple docs with a few minutes of searching: developer.apple.com/library/mac/documentation/General/… Be aware that the answer below is also obsolete, so even SO answers are not immune. Doing your own research before asking someone else is always constructive.Beestings
C
71

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.

Caruthers answered 19/10, 2011 at 19:51 Comment(4)
"...you will want retain or copy in almost all cases. Assign performs no reference count operations." -> Is that still true with ARC? I would have assumed that with ARC there is no need for reference count operations in properties any more?Purapurblind
@Purapurblind i've updated it for ARC. ARC uses reference counting as well -- your reference count operations are added by the compiler.Caruthers
Thanks for the update ... but your answer is still not fully clear for me now. As I gather from the clang-link you added and from this SO answer: https://mcmap.net/q/15482/-in-objective-c-with-arc-is-it-true-that-we-usually-only-need-to-specify-nonatomic-as-property-attributes, strong and not retain is the default since LLVM 3.1 for properties when using ARC ... so there is no need for me as a programmer to declare strong explicitly when I am using ARC. Would you agree?Purapurblind
@Purapurblind The rules are more complex than they used to be. I will update the answer when I have time, so that the case where a strong reference may be implicit/default, rather than assign.Caruthers
M
8

it is equal as

@property (atomic, readwrite, assign) float value;
Mythopoeia answered 19/10, 2011 at 19:56 Comment(0)
H
3

Objective-C property attributes

[Objective-C property]

In Objective-C, when you write @property, after you write a list of attributes

default attributes:

  • pointer: strong, atomic, readwrite

  • primitive: assign, atomic, readwrite

    @property (atomic, readwrite, strong) NSString *someString;
    @property (atomic, readwrite, assign) int someInt;
    

Atomicity is only one part of thread-safe - Atomic Operations[About]

  • atomic (default) - which guarantee that you will not get some junk memory, always it will include some value.

  • nonatomic - you are able to get a garbage data in multi-thread envirompment, but it is a little bit faster for lack of additional logic

Access - modify access

  • readwrite (default) - getter and setter are generated and they can be used

  • readonly - only getter is generated. You are not able to set some value

Storage - memory management for backed instance variables (backing iVar)[About] which are generated automatically

  • strong (default for pointer) - object can not be deallocated while strong backed iVar has a reference to it

  • retain historically was created before strong with the same idea. the difference is that retain should be used when ARC is disabled

  • assign (default for primitive) - out of reference counting

  • weak - this reference is not get into account (+1) as strong do. That is why when all others strong references go out of our object, our reference will be point on nil. It is used to fight with Retain cycle and delegate pattern is a good example[About]

  • unsafe_unretained - similar to weak but when no one reference to object, our iVar will be point on some garbage(dangling pointer) and behaviour is not expected. It can have a little bit better performance than weak

  • copy - a new copy of object is assigned to iVar (NSCopying protocol) when you pass some object into the setter of our property

[Swift weak vs unowned]

Herpetology answered 19/2, 2021 at 17:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.