Question

Two similar questions

Question 1: I’ve a custom setter called mySetter:, is stockName still hold strong pointer?

@property (nonatomic, strong, setter = mySetter:) NSString *stockName;
- (void)mySetter:(NSString *)name {
    _stockName = name;
}

Question 2: I’ve override default setter, is stockName still hold strong pointer too?

@property (nonatomic, strong) NSString *stockName;
- (void)setStockName:(NSString *)stockName {
    _stockName = stockName;
}

Thanks for reply

Was it helpful?

Solution

From Encapsulating Data in "Programming with Objective-C":

Note: The compiler will automatically synthesize an instance variable in all situations where it’s also synthesizing at least one accessor method. If you implement both a getter and a setter for a readwrite property, or a getter for a readonly property, the compiler will assume that you are taking control over the property implementation and won’t synthesize an instance variable automatically.

Since you don't implement a getter, the instance variable _stockName is synthesized. For this it is irrelevant if you use the default or a custom name for the setter.

And declaring the property as "strong" implies __strong ownership for the associated instance variable, see "4.1.1. Property declarations" in the "Clang/ARC" documentation.

OTHER TIPS

If you are using ARC and you specified strong access specifier, property will hold a strong reference. ARC will be doing the retain/release. Under non arc overriding the accessor method is not like this. You should handle the retain/release. By setter = mySetter: you are just assigning an alias name to the accessor method. That will not make any difference to the implementation. Both methods are doing same thing. and one more thing mySetter: will not be the setter for stockName it would be setStockName

stockName will hold strong pointer as you are providing the property with strong ownership. So in both cases you will get a strong pointer reference.

But for the question you have asked you can use stockName as an iVar, setter methods and getter methods as

Question 1

iVar = stockName

getter = [self stockName] or self.stockName

setter = [self mySetter:]

Question 2

iVar = stockName

getter = [self stockName] or self.stockName

setter = self.stockName = or [self setStockName:]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top