如何在IOS 9中使用swift将选定联系人存储到阵列中

如何在IOS 9中使用swift将选定联系人存储到阵列中,ios,arrays,Ios,Arrays,我获取了联系人的详细信息,并希望将其存储到一个数组中。我尝试过,但它像单个联系人一样存储为一个数组。但是我想把它们都存储到一个数组中。如何储存,请指导我。 首先,它会显示我们手机上的所有联系人,我选择了特定的联系人。所选联系人应存储在一个数组中 多谢各位 import UIKit import ContactsUI class ViewController: UIViewController ,CNContactPickerDelegate { let contactStore = C

我获取了联系人的详细信息,并希望将其存储到一个数组中。我尝试过,但它像单个联系人一样存储为一个数组。但是我想把它们都存储到一个数组中。如何储存,请指导我。 首先,它会显示我们手机上的所有联系人,我选择了特定的联系人。所选联系人应存储在一个数组中 多谢各位

import UIKit
import ContactsUI

class ViewController: UIViewController ,CNContactPickerDelegate {
    let contactStore = CNContactStore()
    let amount = 200
    //var selectedContact:CNContact = CNContact()
    //var results:[CNContact] = []
    var res:CNContact = CNContact()
    var a = [String]()
    override func viewDidLoad() {
        super.viewDidLoad()
         perform(#selector(contactPicker(_:didSelect:)), with: nil, afterDelay: 0)
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func viewDidAppear(_ animated: Bool) {
         super.viewDidAppear(animated)

    }


    @IBAction func contact(_ sender: Any) {
        let cnPicker = CNContactPickerViewController()
        cnPicker.delegate = self
        self.present(cnPicker, animated: true, completion: nil)
        do {
            try contactStore.enumerateContacts(with: CNContactFetchRequest(keysToFetch: [CNContactGivenNameKey as CNKeyDescriptor, CNContactFamilyNameKey as CNKeyDescriptor, CNContactMiddleNameKey as CNKeyDescriptor, CNContactEmailAddressesKey as CNKeyDescriptor,CNContactPhoneNumbersKey as CNKeyDescriptor])) {
                (contact, cursor) -> Void in
                self.res = contact
                ///let data = Data(name: contact.givenName)
                //self.dataArray?.addObject(data)
            }
        }
        catch
        {
            print("Handle the error please")
        }
    }
    func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {
        contacts.forEach { contact in
            //let storyboard = UIStoryboard(name:"Main",bundle:nil)
            a = [contact.givenName]
            print("name of contacts in array \(a)")
            let n = a.flatMap({$0}).count
            print(n)
            let res = amount/n
            print(res)
            for c in a {
                print ("split money \(c):\(res)")
            }
            //print(a.count)
            let displayViewController = self.storyboard?.instantiateViewController(withIdentifier: "DisplayViewControllerr")as! DisplayViewControllerr
              displayViewController.b = a
            let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
            appDelegate.window?.rootViewController = displayViewController
            self.navigationController?.pushViewController(displayViewController, animated: true)
            //self.present(displayViewController,animated:false,completion: nil)






            }
    }


    func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
        print("Cancel Contact Picker")
    }



}
#导入
CNContactStore*store=[[CNContactStore alloc]init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL已授予,n错误*\u可为空错误){
如果(已授予==是){
//具有抓取属性的键
NSArray*keys=@[CNContactFamilyNameKey、CNContactGivenNameKey、CNContactPhoneNumber、CNContactImageDataKey];
NSString*containerId=store.defaultContainerIdentifier;
NSPredicate*谓词=[CNContact谓词ForContactsInContainerWithiIdentifier:containerId];
n错误*错误;
NSArray*cnContacts=[store unifiedContactsMatchingPredicate:predicate keystefetch:keys error:&error];
如果(错误){
NSLog(@“获取联系人时出错%@”,错误);
}否则{
NSLog(@“联系人数组%@”,cnContacts.description);
}
}
}];

我得到了答案,并成功地将联系人存储到阵列中。只需修改程序中的一行,即

func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {
        contacts.forEach { contact in
            a+= [contact.givenName]//I changed at this place 
}
print(a)

它将所有选定的联系人显示到数组中。

不是每次初始化类对象时都初始化新数组,而是用联系人填充i,然后将其推入数组中。。。顺便问一下,你的代码在哪里?我们怎样才能从理论描述上帮助你呢?用迅捷的语言展示你尝试过的代码。@Rushabh
func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {
        contacts.forEach { contact in
            a+= [contact.givenName]//I changed at this place 
}
print(a)