c# - 在 iPhone 中获取重复电话号码的理想方法是什么 - MonoTouch

标签 c# iphone ios xamarin.ios abaddressbook

我正在尝试获取 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;
    }

最佳答案

这在我的 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();
                }

            }
        });

关于c# - 在 iPhone 中获取重复电话号码的理想方法是什么 - MonoTouch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14761980/

相关文章:

c# - 是否有任何用于扭曲/扭曲图像的 .NET 工具?

c# - 复选框的 If 语句

iOS 如何知道我正在穿越特定位置

iphone - SQLite MYSQL 字符编码

ios - Xcode 无法使具有标识符的单元格出列

objective-c - 我无法将我的数据对象添加到我的 NSArray

ios - NSNetServiceBrowsing 无法解析服务并出现错误 -72004

c# - 通过哈希码获取对象

c# - 防止在代码中使用 system.reflection

iphone - itunesconnect iPhone 应用程序状态 - 无效的二进制文件