This is the 8th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

preface

What are attribute variables and what are member variables in ios? What is an instance variable?

What’s the connection between them?

What is the specific method of use?

When do you need attribute variables? When do you use member variables?

What are member variables and instance variables

  1. Member variables are variables declared in {}
  2. A member variable is called an instance variable if its type is a class
  3. Member variables include instance variables, so they are generally called member variables.
  • For example, in.hThe header file{}Defined in the
#import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN @interface Persion : NSObject{ NSString *name; // Instance variable int age; } @end NS_ASSUME_NONNULL_ENDCopy the code

Where name is the instance variable and age is the member variable

  • For example, in.mIn the file{}Defined in the
#import "Persion.h" @interface Persion () { NSString *gender; // Int height; } @end @implementation Persion @endCopy the code

Gender is the instance variable and height is the member variable

What are attribute variables

Usually the variables we declare with @property are called property variables

  • For example, in the.h header file

    #import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN @interface Persion : NSObject @property (nonatomic, copy) NSString *info; // Attribute variable @end NS_ASSUME_NONNULL_ENDCopy the code

    The info variable is the attribute variable

  • For example, in a.m file

    #import "Persion.h" @interface Persion () @property (nonatomic, copy) NSString *hobby; // Attribute variable @endCopy the code

    The Hobby variable is the property variable

What’s the connection between them

Say: between you what relation, exactly who seduce who…

Manual dog head. Ha ha

Take off your clothes and look closely and there’s some connection

  • When we create an attribute variable, the system automatically creates a corresponding member variable

    For example:

    #import "Persion.h" @interface Persion () @property (nonatomic, copy) NSString *hobby; @end@implementation - (instanceType)init {if (self = [super init]) {self->_hobby = @" I love you "; } return self; }Copy the code

    After creating a property variable called hobby, we can use the member variable _hobby to assign the value, indicating that the corresponding member variable has been created

How to use a member variable

create

As stated above, variables created in {} are member variables

  • For example, in.hThe header file{}Defined in the
#import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN @interface Persion : NSObject{ NSString *name; // Instance variable int age; } @end NS_ASSUME_NONNULL_ENDCopy the code

Where name is the instance variable and age is the member variable

  • For example, in.mIn the file{}Defined in the
#import "Persion.h" @interface Persion () { NSString *gender; // Int height; } @end @implementation Persion @endCopy the code

Gender is the instance variable and height is the member variable

  • None of the above four member variables, either created in.m or.h, can be accessed from other classes outside the class and an error will be reported:

Instance variable ‘name’ is protected. Instance variable ‘name’ is protected.

Member variables are created with @protected by default, which means they are protected and cannot be accessed externally

  • @private, @protected, @public

    @protected, accessible only in this class or subclass, default type

    @private Private, accessible only in this class

    @public Public can be accessed externally

    Let’s modify the above example and change name to @public:

    @interface Persion : NSObject { @public NSString *name; // Instance variable int age; // Member variable} @endCopy the code

    As you can see, it is accessible in Main.

    Note that @public does not apply to member variables in.m files and cannot be accessed externally

use

Member variables can only be assigned and evaluated by the -> symbol

  • At sign synthesize makes a getter/setter method

    By default, member variables have no getters or setters. You can use the @synthesize declaration to tell the system to create getters or setters for you

How to use attribute variables

create

Property variable creation requires @property

  • For example, in the.h header file

    #import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN @interface Persion : NSObject @property (nonatomic, copy) NSString *info; // Attribute variable @end NS_ASSUME_NONNULL_ENDCopy the code

    The info variable is the attribute variable

  • For example, in a.m file

    #import "Persion.h" @interface Persion () @property (nonatomic, copy) NSString *hobby; // Attribute variable @endCopy the code

    The Hobby variable is the property variable

  • The system automatically creates getter/setter methods for property variables

use

Because you have getter setter methods created, you can use property variables directly using dot syntax

  • Attribute variables created in.m can only be used inside the class, not outside it