Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are private variables in Objective-C strong?

So searching around Stack Overflow this seems to be how to make private variables in Objective-C:

@interface ClassName()
{
@private
    NSArray* private;
}
@property (strong, nonatomic) NSArray* public;
@end

Now this is where I get confused. The property is declared as (strong, nonatomic), but the private variable has nothing of the sort. So how does arc know if it's strong or not?

like image 241
sinθ Avatar asked Mar 24 '13 16:03

sinθ


People also ask

Should instance variables be public or private?

The instance variables are visible for all methods, constructors, and block in the class. Normally, it is recommended to make these variables private (access level). However, visibility for subclasses can be given for these variables with the use of access modifiers. Instance variables have default values.

Why is it a good idea to make all instance variables private?

Instance variables are made private to force the users of those class to use methods to access them. In most cases there are plain getters and setters but other methods might be used as well.

How do I set variables in Objective-C?

Variable Declaration in Objective-C You will use extern keyword to declare a variable at any place. Though you can declare a variable multiple times in your Objective-C program but it can be defined only once in a file, a function or a block of code.

What is a property in Objective-C?

The goal of the @property directive is to configure how an object can be exposed. If you intend to use a variable inside the class and do not need to expose it to outside classes, then you do not need to define a property for it. Properties are basically the accessor methods.


1 Answers

Instance variables are __strong by default.

From Apple's ARC Transition Guide, regarding variables (presumed to include instance variables):

__strong is the default

and later:

With ARC, instance variables are strong references by default—assigning an object to an instance variable directly does extend the lifetime of the object

This holds until the property is connected to the ivar via @synthesize. At this point, the ownership qualifier of the property takes precedence. However, if you declare a property as anything but strong, and then implement both the setters and getters by hand, you'll have to manually declare the backing ivar's ownership qualifier as well.

like image 135
Carl Veazey Avatar answered Oct 02 '22 19:10

Carl Veazey