0

这是我用来从通讯录中获取联系人姓名和电子邮件的功能。

-(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 有基本的了解,但不详细了解如何实现这一点。你能帮我创建它吗?

4

5 回答 5

2

使用 NSMutableDictionary 的setObject:forKey:方法。

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

官方文档,这个方法:

将给定的键值对添加到字典中。

例如,我们可以修改您的代码以创建所需的字典数组。NSMutabeDictionary我们为 for 循环的每个索引 创建一个对象,并不断将其添加到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 上写的,所以请原谅我的任何错别字。

于 2013-10-16T10:22:36.437 回答
1

为你的第一部分做了。请试试 :-

    NSDictionary *countriesListedByLetter = @{@"id" : @"0", @"name" : @"John Appleseed", @"email" : @"John-Appleseed@mac.com", @"selectedFlag": @"NO"};
        NSLog(@"%@",countriesListedByLetter);

OUtPUt:--
{
    email = "John-Appleseed@mac.com";
    id = 0;
    name = "John Appleseed";
    selectedFlag = NO;
}
于 2013-10-16T10:41:23.283 回答
0

尝试查看字典数组。

NSMutableArray *allEmails = [[NSMutableArray alloc] initWithCapacity:_peopleList.count];
    for (int i = 0; i < _peopleList.count; i++)
    {
        NSMutableDictionary *dict = [[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]];

        NSLog(@"id:%d,name:%@",i,name);
        [dict setObject:[NSNumber numberWithInt:i] forKey:@"id"];
        [dict setObject:[NSNumber numberWithBool:false] forKey:@"seletedFlag"];
        [dict setObject:name forKey:@"name"];
        for (int j=0; j < ABMultiValueGetCount(emails); j++)
        {
            NSString* email = (__bridge NSString*)ABMultiValueCopyValueAtIndex(emails, j);
               [dict setObject:email forKey:@"email"];
            NSLog(@"id:%d,email:%@",i,email);
        }
        [allEmails addObject:dict];
        [dict release];

    }


}

  NSLog(@"%@",allEmails);
于 2013-10-16T10:28:54.597 回答
0

尝试这个:

-(void) fetchFriendsAllDetails
{

    NSMutableArray *allEmails = [[NSMutableArray alloc] initWithCapacity:_peopleList.count];
    for (int i = 0; i < _peopleList.count; i++)
    {
        NSMutableDictionary *addressesDict = [[NSMutableDictionary alloc] initWithCapacity:4];
        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);

        [addressesDict setValue:[NSString stringWithFormat:@"%i",i] forKey:@"id"];
        [addressesDict setValue:name forKey:@"name"];


       for (int j=0; j < ABMultiValueGetCount(emails); j++)
         {
             NSMutableString *emailString = [[NSMutableString alloc]init];
             NSString* email = (__bridge NSString*)ABMultiValueCopyValueAtIndex(emails, j);
            [emailString appendString:email];

           if(j != ABMultiValueGetCount(emails) - 1)
           {
           [emailString appendString:@","];
           } 
          [allEmails addObject:emailString];
          [emailString release];

        }

        [addressesDict setValue:@"NO" forKey:@"selectedFlag"];
        [allEmails addObject:addressesDict];

    }

    NSLog(@"RESULT: %@",allEmails);

}
于 2013-10-16T10:29:27.460 回答
0

看起来你想要一个字典数组。

-(void) fetchFriendsAllDetails
{
    NSMutableArray *allContacts = [[NSMutableArray alloc] initWithCapacity:_peopleList.count];
    for (NSUInteger 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);

        NSUInteger count = ABMultiValueGetCount(emails);
        NSMutableArray *emailsM = [[NSMutableArray alloc] initWithCapacity:count];
        for (NSUInteger j=0; j < ABMultiValueGetCount(emails); j++)
        {
            NSString* email = (__bridge NSString*)ABMultiValueCopyValueAtIndex(emails, j);
            [emailsM addObject:email];
//            NSLog(@"id:%d,email:%@",i,email);
        }
        [allContacts addObject:@{@"id": @(i),
                                 @"name": name,
                                 @"email": [NSArray arrayWithArray:emailsM],
                                 @"selectedFlag": @(NO)}];
    }
}
于 2013-10-16T10:33:06.040 回答