Properties in OC.

concept

@property I’m sure you’re all familiar with it. In iOS development, @property is used to declare objects or properties, and objects declared using @property can be invoked via point syntax. A few years ago, at sign property would have been used with at sign synthesize, allowing the compiler to synthesize accessor methods. However, the auto-synthesizing accessor methods have become the default.

What is the difference between @property Object * Object and Object *object1? Object1 is an instance variable that exists in Object instance memory. Object is the property of the object, which can be accessed using point syntax. Most properties are accessed based on instance variables (IVAR). In simple terms, object contains getter and setter methods and the _object instance variable, while object1 is just an instance variable and does not contain accessor methods

attribute

Properties can be divided into four categories:

* Thread safety: Atomic /nonatomic * Access permission: Readonly/ReadWrite * ARC: Assign /weak/strong/copy * MRC: The assign/retain/copy/unsafe_unretained * specified accessor methods: getter/setter = =Copy the code

Thread safety

  • Atomic: Atomic is one of the default values for property. Atomicity ensures that this property is thread-safe. If thread A calls the getter when thread B calls the setter, thread A gets A mutable value. Atomic is not commonly used because it affects performance and is less efficient. If you use atomic and want to implement custom accessor methods, you must implement both getter and setter methods.

  • Nonatomic: Nonatomic is the opposite of atomic. If thread A gets A non-atomic string, thread B changes the character to “Apple” and thread C changes the character to “Pen”, the value obtained by thread A is uncertain and may crash the program. Non-atomicity is not thread-safe, but fast enough.

Access permissions

  • Readwrite: One of the property defaults. Tells the compiler to generate getters and setters

  • Readonly: Tells the compiler to generate only getters. In general, if you want to expose this property to other classes, declare it as readonly in the.h file and readwrite in the.m file. In this case, other external files can only be retrieved, not modified.

Memory management

  • Strong: One of the default values of property. Declaring an attribute as strong increases the reference count every time it is used (ARC does this for us, but in the MRC era needed to count itself). That is, the instance variable holds the property in memory until the reference count reaches zero. Using strong can cause a reference loop, but we can use weak to solve this problem

  • Weak: Given a pointer to an object that does not increment the reference count. If this property is strongly referenced by another class or object, it remains a valid pointer. It immediately becomes nil if there’s no other object to reference it. This avoids the reference loop.

  • Assign: Often applies to primitive types such as int and long. Similar to the weak

  • Copy: Similar to strong, except that it copies the value of the object and strongly references the template. The object to be copied must comply with the NSCopying protocol

  • Retain: Older version of strong.

  • Unsafe_unretained: Similar to weak, but does not set the value to nil if no other object references it

Modifies the accessor method name

  • Getter =: Indicates the getter name of the custom accessor.
  • Setter =: Setter name for the custom accessor.

Swift

After Xcode 6.3, nonNULL, NULlable, and NULl_resetable were added to correspond to Optional types in Swift.