Attributes: @property (strong, nonatomic) NSArray *dataArr;
Rewrite the getter method (the code is about lazy loading, but this is not the point)
- (NSArray *)dataArr{
//1. 判断是否为空
if(_dataArr == nil){ //不能写self.dataArr
self.dataArr = @[ //可以写self.dataArr
.........
];
}
return _dataArr;
}
What I know now is that using self.dataArr
will call the attribute's getter方法
and setter方法
So I think if in parentheses cannot be used self.dataArr
otherwise it will be an infinite loop
But I don’t know why you can use self.dataArr
in if braces?
Is it because the curly brackets are assignments, so only setter方法
will be called?
Can I write self.dataArr
after return?
It’s a bit confusing, please give me some answers...
. . .
Inside the braces
self.dataArr = @[]
will only adjust the setter, so there is no problem, but return self.dataArr; will call the getter, causing an infinite loopYou can log it yourself or track the breakpoints to find out.
Suitable tutorial