我想检查是否存在联系人,如果有,请更改其电话号码。
我尝试这样做,并在最后一行代码中获得exc_bad_access
:
if doesPersonExistWithFirstName(firstName: "Call Recorder", inAddressBook: adbk)
{
let existingContact: ABRecord! = ABAddressBookGetPersonWithRecordID(adbk, recordID).takeRetainedValue()
var success: Bool = false
let phoneNumbers: ABMutableMultiValue = ABMultiValueCreateMutable(ABPropertyType(kABMultiStringPropertyType)).takeRetainedValue()
var error: Unmanaged<CFError>? = nil
success = ABMultiValueAddValueAndLabel(phoneNumbers, recordServicePhoneNumber!, kABPersonPhoneMainLabel, nil)
print("setting phone number successful? \(success)")
success = ABRecordSetValue(existingContact, kABPersonPhoneProperty, phoneNumbers, &error)
print("adding phone numbers successful? \(success)")
success = ABAddressBookAddRecord(adbk, existingContact, &error)
print("Adbk addRecord successful? \(success)")
success = ABAddressBookSave(adbk, &error)
print("Adbk Save successful? \(success)")
}
else
{
let newContact: ABRecord! = ABPersonCreate().takeRetainedValue()
var success: Bool = false
let newFirstName: NSString = "Call Recorder"
let image: UIImage = UIImage(named: "record")!
let imageData = UIImagePNGRepresentation(image)
let phoneNumbers: ABMutableMultiValue = ABMultiValueCreateMutable(ABPropertyType(kABMultiStringPropertyType)).takeRetainedValue()
var error: Unmanaged<CFError>? = nil
success = ABRecordSetValue(newContact, kABPersonFirstNameProperty, newFirstName as CFTypeRef, &error)
print("setting first name was successful? \(success)")
success = ABMultiValueAddValueAndLabel(phoneNumbers, recordServicePhoneNumber!, kABPersonPhoneMainLabel, nil)
print("setting phone number successful? \(success)")
success = ABPersonSetImageData(newContact, imageData as CFData!, &error)
print("setting image successful? \(success)")
success = ABRecordSetValue(newContact, kABPersonPhoneProperty, phoneNumbers, &error)
print("adding phone numbers successful? \(success)")
success = ABAddressBookAddRecord(adbk, newContact, &error)
print("Adbk addRecord successful? \(success)")
success = ABAddressBookSave(adbk, &error)
print("Adbk Save successful? \(success)")
}
} **// I get exception here**
代码正常,电话号码更改正确,但每次运行时应用都会崩溃。
UPD:我跑了僵尸乐器,它给了我这个:
An Objective-C message was sent to a deallocated 'CPRecord' object (zombie) at address: 0x7c6a3200.
UPD2:我发现此行崩溃应用:
let existingContact: ABRecord! = ABAddressBookGetPersonWithRecordID(adbk, recordID).takeRetainedValue()
可能是什么问题?
答案 0 :(得分:0)
正如我想的那样问题在于这一行:
let existingContact: ABRecord! = ABAddressBookGetPersonWithRecordID(adbk, recordID).takeRetainedValue()
我更改了我的功能,现在它使用ABRecord
代替ABRecordID
来访问所需的记录。
固定代码:
if doesPersonExistWithFirstName(firstName: "Call Recorder", inAddressBook: adbk)
{
var success: Bool = false
let phoneNumbers: ABMutableMultiValue = ABMultiValueCreateMutable(ABPropertyType(kABMultiStringPropertyType)).takeRetainedValue()
var error: Unmanaged<CFError>? = nil
success = ABMultiValueAddValueAndLabel(phoneNumbers, recordServicePhoneNumber!, kABPersonPhoneMainLabel, nil)
print("setting phone number successful? \(success)")
success = ABRecordSetValue(existingContact, kABPersonPhoneProperty, phoneNumbers, &error)
print("adding phone numbers successful? \(success)")
success = ABAddressBookAddRecord(adbk, existingContact, &error)
print("Adbk addRecord successful? \(success)")
success = ABAddressBookSave(adbk, &error)
print("Adbk Save successful? \(success)")
}
else
{
let newContact: ABRecord! = ABPersonCreate().takeRetainedValue()
var success: Bool = false
let newFirstName: NSString = "Call Recorder"
let image: UIImage = UIImage(named: "record")!
let imageData = UIImagePNGRepresentation(image)
let phoneNumbers: ABMutableMultiValue = ABMultiValueCreateMutable(ABPropertyType(kABMultiStringPropertyType)).takeRetainedValue()
var error: Unmanaged<CFError>? = nil
success = ABRecordSetValue(newContact, kABPersonFirstNameProperty, newFirstName as CFTypeRef, &error)
print("setting first name was successful? \(success)")
success = ABMultiValueAddValueAndLabel(phoneNumbers, recordServicePhoneNumber!, kABPersonPhoneMainLabel, nil)
print("setting phone number successful? \(success)")
success = ABPersonSetImageData(newContact, imageData as CFData!, &error)
print("setting image successful? \(success)")
success = ABRecordSetValue(newContact, kABPersonPhoneProperty, phoneNumbers, &error)
print("adding phone numbers successful? \(success)")
success = ABAddressBookAddRecord(adbk, newContact, &error)
print("Adbk addRecord successful? \(success)")
success = ABAddressBookSave(adbk, &error)
print("Adbk Save successful? \(success)")
}
existingContacts
实施:
var existingContact: ABRecord! = ABPersonCreate().takeRetainedValue()
func doesPersonExistWithFirstName(firstName paramFirstName: String,
inAddressBook addressBook: ABAddressBook) -> Bool
{
let people = ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue() as NSArray as [ABRecord]
for person: ABRecord in people
{
let firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty).takeRetainedValue() as! String
if firstName == paramFirstName
{
existingContact = person
return true
}
}
return false
}