Object C: declaration of @property declaration and declaration of instance variable

Although the question is basic, it’s very important for me to understand how to continue programming iOS. Sometimes we used only an instance variable to declare, and we do not set any property associated with it. Some where we simply declare properties and use synthesis to get or set values. Sometimes I need to declare both codes when compilation gives me warnings! What is the basis for manipulating properties in Objective C. I know the basic requirement for creating a getter and setter for any instance variable, but when? I have seen this many times when we do not use the property at all, and after that we easily set and get the value of the variable. In addition, various types of property, such as atomic, non-atomic, strong, preserving, are very incomprehensible to me. Speeding up XCODE to 4.2 shocked my memory management concepts. Can someone please clear the cloud of my mind?

+4
source share
2 answers

Properties are always the preferred way to access ivar directly, mainly for the following reasons:

  • You can override a getter or setter in a subclass
  • You can define "behavior assignment" (namely copy , assign , retain/strong , weak )
  • You can synchronize ivar access

Keywords:

  • copy : the object is copied to ivar during installation
  • assign : the object pointer is assigned ivar during installation
  • retain/strong : object is saved in set
  • weak : In ARC, this is similar to the destination, but will be automatically set to nil when the instance is freed, also used in the garbage collection environment.
  • nonatomic : Accessor is not @synchronized (threadsafe) and therefore faster
  • atomic : Accessor @synchronized (threadsafe), and therefore slower

Generally, you should always synthesize ivar. If you need faster access for performance reasons, you can always directly access synthesized ivar.

+9
source

When typing, I saw that "Eric Aigner" was faster with a good answer.

For an example on properties, synthesis, and a custom network device, see my answer to the stack: Objective-C convention to prevent "local declaration hiding instance variable" warning

For the ARC stater tutorial, see the explanation on Ray wenderlich on its website:

The beginning of ARC in iOS 5 part 1 and

The beginning of ARC in iOS 5 part 2

+2
source

Source: https://habr.com/ru/post/1397380/


All Articles