合并复制的CNCONTACT阵列swift 3触点框架

时间:2017-09-02 19:22:40

标签: ios swift3 cncontactstore contacts-framework

我确实从这个方法中找到了重复的联系人列表,现在我被困在合并重复项中,任何想法我都能做到这一点。

我使用此代码从前一个问题引用了重复文件。

let formatter = CNContactFormatter()
formatter.style = .fullName

let keys = [CNContactIdentifierKey as CNKeyDescriptor, CNContactFormatter.descriptorForRequiredKeys(for: .fullName)]
let request = CNContactFetchRequest(keysToFetch: keys)
var contactsByName = [String: [CNContact]]()
try! self.store.enumerateContacts(with: request) { contact, stop in
    guard let name = formatter.string(from: contact) else { return }
    contactsByName[name] = (contactsByName[name] ?? []) + [contact]   // or in Swift 4, `contactsByName[name, default: []].append(contact)`
}
let duplicates = contactsByName.filter { $1.count > 1 }

3 个答案:

答案 0 :(得分:1)

如果您在使用此代码合并重复项后,您已按照我之前的答案获取重复项列表。

var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var nodemailer = require('nodemailer');

var app = express();

// tells which folder the template files will be in
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));

app.get('/', function(req, res){
    res.render('index');
});

app.listen(3000);

console.log('Server is running on port 3000');

这种方法需要相当大的记忆,所以我建议,使用这种方法作为参考。

答案 1 :(得分:0)

我对此做了一点修改。也许有帮助。

extension Array where Element == String {

    var bestElement: String? {
        var options: [String : Int] = [:]

        for element in self {
            if let result = options[element] {
                options[element] = result + 1
            } else {
                options[element] = 1
            }
        }

        return options.sorted { $0.1 > $1.1 }.first?.key
    }
}


static func merge(duplicates: [CNContact]) -> CNContact {
    // EMPTY CNCONTACT PROPERTIES
    var givenName: [String] = []
    var familyName: [String] = []
    var organizationName: [String] = []
    var notes: [String] = []

    var phoneNumbers: [CNLabeledValue<CNPhoneNumber>] = []
    var emailAddresses: [CNLabeledValue<NSString>] = []
    var postalAddresses: [CNLabeledValue<CNPostalAddress>] = []
    var urlAddresses: [CNLabeledValue<NSString>] = []

    // COLLECT VALUES
    for contact in duplicates {
        givenName.append(contact.givenName)
        familyName.append(contact.familyName)
        organizationName.append(contact.organizationName)
        notes.append(contact.note)

        contact.phoneNumbers.forEach { phoneNumbers.append($0) }
        contact.emailAddresses.forEach { emailAddresses.append($0) }
        contact.postalAddresses.forEach { postalAddresses.append($0) }
        contact.urlAddresses.forEach { urlAddresses.append($0) }
    }

    // MERGE TO NEW CONTACT
    let newContact = CNMutableContact()
    newContact.givenName = givenName.bestElement ?? ""
    newContact.familyName = familyName.bestElement ?? ""
    newContact.organizationName = organizationName.bestElement ?? ""
    newContact.note = notes.joined(separator: "\n")

    newContact.phoneNumbers = phoneNumbers
    newContact.emailAddresses = emailAddresses
    newContact.postalAddresses = postalAddresses
    newContact.urlAddresses = urlAddresses

    return newContact
}

答案 2 :(得分:0)

在阅读功能时我要问自己两件事。

  1. newContact.phoneNumbers:似乎您附加了2个联系人的所有电话号码。如果两个重复的联系人具有相同的号码,那么newContact在列表中将具有两次相同的号码,对吧?

  2. 感觉就像您正在创建的newContact正在丢失大量信息。如昵称或前缀(医生等)。

仍然要感谢这个紧凑的代码:)