我正在尝试在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;
}
答案 0 :(得分: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();
}
}
});