0

我正在尝试在 iPhone 通讯录上获取重复的电话号码。我的问题是,我的 iPhone 上有 264 个联系人,这个过程需要 16 秒!我已经从 iTunes 购买了应用程序,并且没有时间做同样的过程。所以我相信我的方法是不正确的。我知道在 Xcode 中我应该使用 NSPredicate 。我试图用 c# 谓词做到这一点,但结果相同。以下是我获取重复电话号码的代码:

                ABAddressBook ab = new ABAddressBook ();
    OrderedDictionary persons = new OrderedDictionary ();

            foreach (ABPerson p in ab.GetPeople()) {

            foreach (var phoneNumber in p.GetPhones().GetValues()) {
                var duplicates = SearchByPhoneNumber (ab, phoneNumber);
                if (duplicates.Count > 1) {
                    if (!persons.Contains (phoneNumber)) {
                        persons.Add (phoneNumber, duplicates);
                    }
                }

            }
        }

     private  List<ABPerson> SearchByPhoneNumber ( ABAddressBook ab, string phoneNumber)
    {
        List<ABPerson> duplicatepeople = new List<ABPerson> ();
        phoneNumber = Regex.Replace (phoneNumber, "[^0-9]", "");

        var people = ab.Where(x=> x is ABPerson).Cast<ABPerson>()
        .Where((e)=> { return (e.GetPhones().Any((p)=>{return (Regex.Replace(p.Value,"[^0-9]", "")==phoneNumber); }));}).ToList ();

        return people;
    }
4

1 回答 1

1

这在我的 iPhone 4s 上运行大约 150 毫秒。

        NSError err;
        ABAddressBook ab = ABAddressBook.Create(out err);
        Dictionary<string,List<string>> phones = new Dictionary<string,List<string>> ();

        ab.RequestAccess(delegate(bool granted,
                               NSError error) {
            if (granted)
            {
                foreach (ABPerson p in ab.GetPeople()) {
                    foreach (var phoneNumber in p.GetPhones().GetValues()) {
                        if (phones.ContainsKey(phoneNumber))
                        {
                            phones[phoneNumber].Add (p.FirstName + " " + p.LastName);
                        }
                        else
                        {
                            phones.Add (phoneNumber,new List<string>() { p.FirstName + " " + p.LastName });
                        }

                    }

                }

                var dupes = (from x in phones where x.Value.Count() > 1 select x);

                foreach(var d in dupes)
                {
                    Console.Write(d.Key + ": ");

                    foreach (string s in d.Value)
                    {
                        Console.Write(s + ", ");
                    }

                    Console.WriteLine();
                }

            }
        });
于 2013-02-07T23:17:33.217 回答