在我的init方法中使用点表示法将retain属性初始化为nil是不是一个坏主意?
对于像这样的普通房产:
@property (nonatomic, retain) id foo;
Run Code Online (Sandbox Code Playgroud)
在我设置的init方法中说self.foo = nil.合成的方法首先释放或自动释放foo(不完全确定潜在的实施).被fooguaranted第一setter或getter调用之前是零?或者它会指向随机垃圾,除非我明确设置foo = nil没有点符号?
initialization properties reference-counting objective-c dealloc
这是我的源代码
- (id)initWithCollectionView:(UICollectionView *)collectionView
{
self = [super init];
if (self)
{
self.collectionView = collectionView;
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
[self.collectionView registerClass:[TWNTweetCell class] forCellWithReuseIdentifier:kCell];
self.collectionViewLayout = self.collectionView.collectionViewLayout;
self.tweetArray = @[];
self.tweetTextArray = @[];
self.twitter = [STTwitterAPI twitterAPIOSWithFirstAccount];
}
return self;
}
#pragma mark - CollectionView
#pragma mark DataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection: (NSInteger)section
{
return [self.tweetArray count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
TWNTweetCell *cell = (TWNTweetCell *)[collectionView dequeueReusableCellWithReuseIdentifier:kCell forIndexPath:indexPath];
NSDictionary …Run Code Online (Sandbox Code Playgroud) 我正在阅读有关内存管理的一些内容,他们不建议在初始化方法中使用访问器方法.
问题:为什么我们不应该在initilizer方法中使用访问器方法
我正在stackoverflow和其他网站上搜索有关此问题的引用.但是,我并不是很困惑.
任何人都可以就这个问题向我提出建议,或者你是否可以向我提出一个例子或一个好的参考资料,以便我可以自己去做.谢谢
这是我的代码:
class Base
{
init(){
print("Super!")
}
}
class Test : Base
{
internal var y:Int
convenience init(_ a:Int)
{
self.init()
print("\(a)")
}
override init()
{
super.init() //Error!!! Property 'self.y' not initialized at super.init call
y = 123
}
}
Run Code Online (Sandbox Code Playgroud)
我认为应该编译:
y在'Base'类中是不可见的,无论是y's'和超类的初始化顺序是否真的无关紧要.