简体   繁体   中英

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.

In the viewDidLoad I do the fetch request and set the returned array of NSManagedObjects to a class variable 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.

I managed to fix the problem by setting the array from the fetch request by adding self to the above line of code:

    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”属性很可能是保留属性,因此在第一种情况下,该数组没有保留,在您访问该数组时已被释放。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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