Understanding Properties and Instance Variables

Asked 2 years ago, Updated 2 years ago, 77 views

I have a question about properties and instance variables.

------- Sample 1

#import "ViewController.h"

@ interface ViewController()

@property(weak, nonatomic)IBOutlet UITextField*text1;

@end

@implementation ViewController

-(void)viewDidLoad{
    superviewDidLoad;
    self.text1.text=@"test";
}

------- Sample 2

#import "ViewController.h"

@interfaceViewController(){
    __weak IBOutlet UITextField*text1;
}

@end

@implementation ViewController

-(void)viewDidLoad{
    superviewDidLoad;
    text1.text=@"test";
}

If you do the above two, both of them will show test in TextField.

Sample 1 also appears in _text1.text=@"test".
I understand what this means.

However, I don't understand how to use it, whether to declare it as a property or how to use it.
I'm getting lost and wondering what to do.

Could someone please tell me?Thank you for your cooperation.

Regarding the environment,
OS X 10.9.5 Xcode 6.1.1
That's it.

objective-c

2022-09-30 18:14

2 Answers

There are two possible responses depending on whether Automatic Reference Counting (ARC) is enabled.Assume ARC is enabled first.

@property is a syntax sugar that provides a setter/getter declaration (and, if necessary, its implementation) of the property with the specified name along with its description.
Properties and setter/getter are one of the concepts (abstracting techniques) to achieve encapsulation.Motivation to use it is the external specification (interface) of the object you are trying to create.
It comes from applying the 属性something made up of a set of attributes の model to the , and then providing a way to read and write attribute values to that interface.

On the other hand, the instance variable is an instance-aware variable that can be used by class makers to describe features in order for objects to function as desired.It's just a container.Basically, you can only browse/update objects, and if not, they are not objects because they are not fully abstracted in the form of objects.

Therefore, it is better to write instance variables in @implementation{} in the source code file (.m, .mm) than in @interface{} on the header (.h) describing the external specification.

To summarize the characteristics of each, I think the following is the case.Please use it as a reference when using it differently.

  • properties (=@property)

    • How to represent external specifications (interfaces) of objects

    • Declared in the header file (.h)

  • instance variable

    • Use (container) for algorithms to describe object functionality

    • Only visible from the source file (.m, .mm)


  • How to represent external specifications (interfaces) of objects

  • Declared in the header file (.h)


  • Use (container) for algorithms to describe object functionality

  • Only visible from the source file (.m, .mm)

In a world where ARC is disabled, the substitution of variables does not automatically retain/release objects, so you must manage them yourself.
@property(retain) has the ability to automatically retain/release properties when they are set to properties, making it easier than using a normal instance variable


2022-09-30 18:14

Now that memory management with ARC is standard, there is no particular difference between properties and instance variables.The perception that "properties are just sugar-coated syntax that provides accessories to instance variables" is generally correct.

However, you should use properties rather than instance variables.

Declared

Property declarations have the advantage of being able to describe what nature they have.

@property(copy)NSNumber*value;

The above properties declare that the value will be copied and retained.

@property(strong)NSObject*deprecated DEPRECATED_ATTRIBUTE;

You can declare that utilization is deprecated.Code using properties displays a warning.

This expression is not present in the instance variable.

Enables you to hide your implementation

Instance variables are implementation details.The property is API.

For example, suppose you substitute nil for an instance variable.

value=nil;

If the instance variable value has any inconvenience keeping nil, what should we do?In most cases, it cannot be addressed.

In contrast, properties are accessed by methods, so

self.value=nil;

The description can be detoxified by replacing accessories, for example:

-(void)setValue:(NSValue*)aValue
{
  if(aValue){
    _value = aValue;
  } else {
    _value=@0;
}

There are many applications, such as delaying initialization.

Disadvantages and Benefits

Properties are not flawless at all.Always through accessories, resulting in overhead for function calls.Compiler optimization is sufficiently negligible for the most common application creation.

The good habit of programming to the interface rather than to the implementation details is much more beneficial.Even if it is a closed area that is not accessed from the outside, uniform notation to properties increases readability.


2022-09-30 18:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.