iphone - 如何为地址簿中的联系人创建具有多种类型的 NSMutableDictionary?

标签 iphone ios objective-c nsmutablearray nsmutabledictionary

这是我用来从地址簿中获取联系人姓名和电子邮件的函数。

-(void) fetchFriendsAllDetails {

    NSMutableArray *allEmails = [[NSMutableArray alloc] initWithCapacity:_peopleList.count];
    for (int i = 0; i < _peopleList.count; i++) {
        ABRecordRef person = (__bridge ABRecordRef)([_peopleList objectAtIndex:i]);
        ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
        NSString *name=[[NSString stringWithFormat:@"%@",(__bridge_transfer NSString *)ABRecordCopyCompositeName(person)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

        NSLog(@"id:%d,name:%@",i,name);

        for (int j=0; j < ABMultiValueGetCount(emails); j++) {
            NSString* email = (__bridge NSString*)ABMultiValueCopyValueAtIndex(emails, j);
            [allEmails addObject:email];
            NSLog(@"id:%d,email:%@",i,email);
        }
    }
}

上面的输出如下:

id:0,name:John Appleseed
id:0,email:John-Appleseed@mac.com

id:1,name:Kate Bell
id:1,email:kate-bell@mac.com
id:1,email:www.icloud.com

id:2,name:Anna Haro
id:2,email:anna-haro@mac.com

id:3,name:Daniel Higgins Jr.
id:3,email:d-higgins@mac.com

id:4,name:David Taylor

id:5,name:Hank M. Zakroff
id:5,email:hank-zakroff@mac.com

我想在上面的函数中创建一个字典,它将包含以下格式的输出

 {
id:0
name:John Appleseed
email:John-Appleseed@mac.com
selectedFlag:NO
},
{
id:1
name:Kate Bell
email:kate-bell@mac.com, www.icloud.com
selectedFlag:NO
},
{
id:2
name:Anna Haro
email:John-Appleseed@mac.com
selectedFlag:NO
},
{
id:3
name:Daniel Higgins Jr.
email:d-higgins@mac.com
selectedFlag:NO
},
{
id:4
name:David Taylor
email:""
selectedFlag:NO
},
id:5
nameHank M. Zakroff
email:hank-zakroff@mac.com
selectedFlag:NO
}

我对 NSMutableDictionary 有基本的了解,但不知 Prop 体如何实现。你能帮我创建它吗?

最佳答案

使用 NSMutableDictionary 的 setObject:forKey: 方法。

- (void)setObject:(id)anObject forKey:(id < NSCopying >)aKey

来自 official documentation ,这个方法:

Adds a given key-value pair to the dictionary.

例如,我们可以修改您的代码来创建所需的字典数组。我们为 for 循环的每个索引创建一个 NSMutabeDictionary 对象,并将其添加到一个 NSMutableArray 对象。

-(void) fetchFriendsAllDetails
{
    // allocate array
    NSMutableArray *array = [[NSMutableArray alloc]init];  
    NSMutableArray *allEmails = [[NSMutableArray alloc] initWithCapacity:_peopleList.count];
    NSMutableDictionary *dictionary;
    for (int i = 0; i < _peopleList.count; i++)
    {
    dictionary  = [[NSMutableDictionary alloc]init];
    ABRecordRef person = (__bridge ABRecordRef)([_peopleList objectAtIndex:i]);
    ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
    NSString *name=[[NSString stringWithFormat:@"%@",(__bridge_transfer NSString *)ABRecordCopyCompositeName(person)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    // create key -vale pair for id and name
   [dictionary setObject:[NSNumber numberWithInt:i] forKey:@"id"]; // here we used int wrapped inside and //object because  NSMutable Dictionary expects an object instead of scalar type int.
   [dictionary setObject:name forKey:@"name"];
    NSLog(@"id:%d,name:%@",i,name);

  // Create an NSMutableString to hold more than one email
    NSMutableString *mutableEmail = [[NSMutableString alloc]init];
      for (int j=0; j < ABMultiValueGetCount(emails); j++)
        {
            NSString* email = (__bridge NSString*)ABMultiValueCopyValueAtIndex(emails, j);
            [mutableEmail appendString:email];
            // append comma to separate more than one mail   
            if(j != ABMultiValueGetCount(emails) - 1)
             {
               [mutableEmail appendString:@","];
             } 
            [allEmails addObject:email];
            NSLog(@"id:%d,email:%@",i,email);
        }
      [dictionary setObject:mutableEmail forKey:@"email"];
      // for boolean also. wrap inside an object
      [dictionary setObject:[NSNumber numberWithBool:NO] forKey:@"id"];
      // add dictionary to array
      [array addObject:dictionary];
    }
}

PS:我是在 Windows 上写的,所以请原谅我的错别字。

关于iphone - 如何为地址簿中的联系人创建具有多种类型的 NSMutableDictionary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19400715/

相关文章:

ios - 检查具有 Apple Pay 功能的设备

iphone - iOS 6 map 偶尔崩溃

iphone - UIActionSheet 委托(delegate)方法不会被调用

ios - nscalendar 有问题

ios - 在导航栏中使用 UIPageControl 时出现 EXC_BAD_ACCESS

iphone - 将 UIView 置于所有其他 View 之上

iphone - 需要在objective-c中定义一个 "modifiable"全局变量

iphone - 在没有导航 Controller 堆栈的情况下,以动画方式将 View Controller 从一个 View Controller 过渡到另一个 View Controller

ios - 使用 NSUserdefaults 注销用户会出现什么错误?

ios - 通过引用另一个类来传递 CGFloat 的最佳方法