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