Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't set datasource programmatically, getting EXC_BAD_ACCESS

I'm trying to set datasource for my table view programmatically, I have a class which I created and it implements (I don't know if implements is the right word for it, new to OC, I come from Java) the UITableViewDataSource protocol.

@interface PlaylistController : NSObject <UITableViewDataSource, UITableViewDelegate>

in my ViewController in the viewDidLoad method I try to assign the datasource, but I get EXC_BAD_ACCESS.

[playlistView setDataSource:[[PlaylistController alloc] initWithPlaylist:playlist]];

And I don't know why?

From reading here (SO) I understand that it either a casting problem (but I do implement the needed protocol), or memory management problem (but I'm using ARC, not touching release at all)

like image 843
RCB Avatar asked Aug 24 '26 14:08

RCB


1 Answers

Instead of

[playlistView setDataSource:[[PlaylistController alloc] initWithPlaylist:playlist]];

Use

[playlistView setDataSource:self];

Reason:

You tried to set DataSource to an instance that is in autorelease. It gets released and you get the Error.

You should set the dataSource to self, so that it points to the current instance of the class.

like image 175
Anoop Vaidya Avatar answered Aug 26 '26 05:08

Anoop Vaidya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!