简体   繁体   English

为什么设置类变量NSArray要求我使用访问器方法?

[英]Why does setting the class variable NSArray require me to use the accessor method?

I'm using a NSFetchRequest to create a section in a UITableView based on objects stored in Core Data. 我正在使用NSFetchRequest基于存储在Core Data中的对象在UITableView中创建一个部分。

In the viewDidLoad I do the fetch request and set the returned array of NSManagedObjects to a class variable NSArray *allEvents. 在viewDidLoad中,我执行获取请求,并将返回的NSManagedObjects数组设置为类变量NSArray * allEvents。 I was setting the array returned from the fetch request like so: 我正在设置从获取请求返回的数组,如下所示:

    allEvents = [context executeFetchRequest:eventFetchRequest error:&error];

I was getting a EXEC_BAD_ACCESS error when I tried calling [allEvents count] in my numberOfRowsInSection later on. 稍后尝试在numberOfRowsInSection中调用[allEvents count]时,出现EXEC_BAD_ACCESS错误。

I managed to fix the problem by setting the array from the fetch request by adding self to the above line of code: 我设法通过在上面的代码行中添加self来设置来自获取请求的数组来解决问题:

    self.allEvents = [context executeFetchRequest:eventFetchRequest error:&error];

Can anyone explain why this worked? 任何人都能解释为什么这有用吗 I've run into similar situations and would love to know what's going on. 我遇到过类似的情况,很想知道发生了什么。

Thanks. 谢谢。

对象上的“ allEvents”属性很可能是保留属性,因此在第一种情况下,该数组没有保留,在您访问该数组时已被释放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM