Whether the IOS OC member variable is a strong or weak reference

Source: Internet
Author: User

The member variable, in this case, is an instance member variable that acts within the entire class object. From the life cycle, it is longer than a local variable, but it is private by default and is inaccessible to other objects.

For example you have a variable placed in @interface  , when you apply for an entity, the variable survival cycle is the same as your entity.

Because the scope of the member variable is within the entire class object, OC inside the member variable should be the strong property.


Links: http://www.zhihu.com/question/23347971/answer/24332408
Example:
header file:
@interfaceViewControllerUIViewController{    //成员变量    NSArray*array1;}//属性变量@property(nonatomic,strong)NSArray*array2;@end


source file:
- (void)viewDidLoad{    [super viewDidLoad];    array1 = [[NSArray alloc] initWithObjects:@"abcde", nil];    array2 = [[NSArray alloc] initWithObjects:@"123abc", nil];    {//        局部变量        NSArray *array1 = [[NSArray alloc] initWithObjects:@"12345", nil];    }    NSLog(@"\narray1 : %@\n array2 : %@",array1,array2);}

my understanding is that
A local variable is defined by its lifetime, in the source file, in the Array1, whose life cycle is in a block of code bounded by "{}", although its name is the same as the member variable, but not the same variable. A member variable is a temporary variable that is used within a region.
The member variable, in this case, is an instance member variable that acts within the entire class object. From the life cycle, it is longer than a local variable, but it is private by default and is inaccessible to other objects. Therefore, the general custom method, as an interface, allows other objects to access this variable. Therefore, member variables are used inside the class without the need to contact the outside variables.
Depending on the private nature of the member variable, the attribute variable is available for easy access. The advantage of a property variable is that it allows other objects to access the variable. Of course, you can set up read-only or writable, and the settings can be customized. Therefore, a property variable is a variable that is used to interact with other objects.
At present I understand so, hope to be helpful to you.

member variables and attribute variables are exactly the same, don't be misled by OBJ-C 2.0 's flexible notation.
But the attribute variable can be a method name, that is, it can have other names.

http://www.cocoachina.com/bbs/read.php?tid-245245.html

See an article:

What is the difference between the member variable definitions in iOS and @interface and @implementation? What is the difference between the member variable definitions in iOS and @interface and @implementation?

A member variable in @interface


@implementation is a global variable


The problem of scope and survival interval


For example, you put a variable in @interface , when you apply for an entity, the variable survival cycle is the same as your entity


In @implementation, because it is global, there is always

Thank you very much!
also want to know, what is the difference between the specific scopes?

Scope, which is the variable in the surviving area,


For example, the member variable is placed in the @interface, scope is the class inside


Global variables, which are the entire program


Temporary variables are typically inside a function


@implementation testclass{
UIView *_test;
}
Such variables should not be global, right?

@implementation testclass{UIView *_test;}


This situation is less, not recommended, this is a member variable


But the default is @private

are actually member variables that are placed in theThe @implemetation is private.  

are member variables, just put in Variables in @implemetation cannot inherit  

The IOS OC member variable is a strong or weak reference

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.