I'm using this open source library which is a wrapper on AddressBook.framework. There's no method for deleting contact, so I'm gonna use default ABAddressBookRemoveRecord
. However for that I need the ABAddressBookRef
.
Since it's declared in APAddressBook.m
file of that APAddressBook
library as
@property (nonatomic, readonly) ABAddressBookRef addressBook;
and I'm not able to get access to it, I wanted to write extension in Swift, containing method returning it.
So this is body of my extension:
extension APAddressBook {
private struct AssociatedKey {
static var addressBookRef = "addressBook"
}
var xo: ABAddressBook! {
get {
return objc_getAssociatedObject(self, &AssociatedKey.addressBookRef)
}
set(newValue) {
objc_setAssociatedObject(self, &AssociatedKey.addressBookRef, newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN))
}
}
func getABAddressBook() -> ABAddressBookRef {
return self.xo
}
}
I've also tried using private var xoAssociationKey: UInt8 = 0
as associatedKey, but calling getABAddressBook()
function always crashes with fatal error: unexpectedly found nil while unwrapping an Optional value.
What's wrong with my code? Also is there better way to solve my problem?