Correct way to set up an addressBook instance variable?
Maybe someone can answer this one easily. I working on an iphone app where to get info for each row in cellForRowAtIndexPath I was creating a new address book each time calling:
ABAddressBookRef addressBook = ABAddressBookCreate();
This works fine other than it's slow and does not feel right to be creating a new ab for each row. So I created an addressBook instance variable but can't seem to use it without crashing. (Program received signal: “EXC_BAD_ACCESS”.) I imagine that I'm not setting it up right, but have have had no luck finding anything specifically about this in the documentation.
Thanks 开发者_JS百科a million in advance to anyone who can help.
In my .h file I did this:
ABAddressBookRef addressBook;
@property(nonatomic, readwrite) ABAddressBookRef addressBook;
Then in my .m file:
-(id)initWithStyle:(UITableViewStyle)style
{
if (self = [super initWithStyle:style]) {
addressBook = ABAddressBookCreate();
}
return self;
}
When do you release the addressBook by CFRelease(addressBook) ? If the memory is not enough, the ABAddressBookCreate() will return a nil object. The C-based addressBook.framework API cannot work with nil object, and it will crash with "EXC_BAD_ACCESS".
精彩评论