我试图从我的人员选择器(iOS)中的联系人那里收到电子邮件,但我遇到了一个小问题(因为Swift 2.0,之前它工作得很好):我想得到它字符串数组中的电子邮件,但是当我打印它时,它看起来像这样:
["(\n \"myEmail1.com\",\n \"myEmail2.com\",\n \"myEmail3\"\n)"]
这是我的代码:
if ((ABRecordCopyValue(person, kABPersonEmailProperty)) != nil) {
let email: ABMultiValueRef = ABRecordCopyValue(person, kABPersonEmailProperty).takeRetainedValue() as ABMultiValueRef
self.contactSelectedEmails = [String(ABMultiValueCopyArrayOfAllValues(email).takeUnretainedValue())]
print(self.contactSelectedEmails)
prefs.setObject(self.contactSelectedEmails, forKey: self.contactSelectedName.stringByReplacingOccurrencesOfString(" ", withString: "").lowercaseString + "_emails")
}
当我打印self.contactSelectedEmails[0]
时,我可以获取我的数组,但我无法将其存储在var中。
感谢您的帮助
答案 0 :(得分:0)
您是否尝试过使用CNContact而不是AB框架?
我最近将所有代码转换为使用CNContact,但我在Swift 2中没有任何问题。
作为参考,下面是一些抓取电子邮件地址并将其保存到核心数据中的代码,但您可以轻松地将其放入数组中。它并没有像我想的那样痛苦地转换为CNContact框架,它实际上使我的代码更简单。
func presentPicker() {
let contactPicker = CNContactPickerViewController()
contactPicker.delegate = self
contactPicker.displayedPropertyKeys = [CNContactEmailAddressesKey]
print("ContactPicker called")
self.presentViewController(contactPicker, animated: true, completion: nil)
}
func contactPicker(picker: CNContactPickerViewController, didSelectContactProperty contactProperty: CNContactProperty) {
let contact = contactProperty.contact
let name = "\(contact.givenName) \(contact.familyName)"
let email = contactProperty.value
let aFetchedRequest = NSFetchRequest(entityName: "Recipients")
let predicate = NSPredicate(format: "name == %@", contact)
aFetchedRequest.predicate = predicate
let recordCheck = managedObjectContext!.countForFetchRequest(aFetchedRequest, error: nil)
if recordCheck > 0 {
//if person is in MOC
let thePerson = (try! managedObjectContext!.executeFetchRequest(aFetchedRequest)) as! [Recipients]
thePerson[0].emailRecipient = "\(email!)"
thePerson[0].name = name
print("does exist name \(thePerson[0].name)")
} else {
//if person isn't in MOC
let newRecipient = NSEntityDescription.insertNewObjectForEntityForName("Recipients", inManagedObjectContext: managedObjectContext!) as! Recipients
newRecipient.emailRecipient = "\(email!)"
newRecipient.name = name
newRecipient.uniqueIdentifier = NSUUID().UUIDString
print("didn't exist name: \(String(name))")
}
var error : NSError? = nil
do {
try managedObjectContext!.save()
} catch let error1 as NSError {
error = error1
NSLog("Unresolved error \(error), \(error!.userInfo)")
abort()
}
do {
try managedObjectContext!.save()
} catch _ {
}
getFetchResultsController()
theTableView.reloadData()
print("Stored number: = \(email!)")
}