如何从 Xcode objective-c 中的 CNContacts 获取街道地址

How do I get just the street address from CNContacts in Xcode objective-c

我正在尝试获取 CNContacts 中所有联系人的街道地址。我已经能够将 givenName 和 familyName 作为 NSString 获取,并且我已经能够将 postalAddress 作为包含街道、城市、邮编等的数组获取,但我只想从数组中获取街道地址作为字符串.
这是我的代码

 CNContactStore *store = [[CNContactStore alloc] init];
                          [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
                              if (granted == YES) {
                                  //keys with fetching properties
                                  NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPostalAddressesKey,CNPostalAddressStreetKey,CNPostalAddressCityKey,CNPostalAddressPostalCodeKey];
                                  NSString *containerId = store.defaultContainerIdentifier;
                                  NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
                                  NSError *error;
                                  NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
                                   if (error) {
                                      NSLog(@"error fetching contacts %@", error);
                                  }  else {
                                      
                                     for (CNContact *contact in cnContacts) {
                                         NSString *firstNames = contact.givenName;
                                          NSString *lastNames = contact.familyName;
                                    
                                         NSMutableArray *streetName = [[NSMutableArray alloc]initWithObjects:contact.postalAddresses, nil];
                                        
                                         NSLog(@"streets:::%@",streetName); }}}}];

我正在使用 Objective-c,很少有使用 Swift 的示例,但没有使用 Objc。 请有人告诉我如何操作。

根据 CNContact 对象 (https://developer.apple.com/documentation/contacts/cncontact/1403066-postaladdresses?language=objc) 的 postalAddresses 属性 的文档,它是这样定义的:

NSArray<CNLabeledValue<CNPostalAddress*>*>* postalAddresses;

这意味着它包含一个 CNLabeledValue 对象数组,每个对象都包含一个 CNPostalAddress。这允许每个邮政地址与描述它的标签一起存储,并且多个地址与相同的标签一起存储。

在上面的屏幕截图中,您可以看到在将地址添加到联系。

CNContactStore* store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts
                completionHandler:^(BOOL granted, NSError * _Nullable error) {

    if (error)
    {
        NSLog(@"Error accessing contacts %@", error.debugDescription);

        return;
    }

    if (granted)
    {
        NSArray* keys = @[ CNContactFamilyNameKey,
                           CNContactGivenNameKey,
                           CNContactPostalAddressesKey,
                           CNPostalAddressStreetKey,
                           CNPostalAddressCityKey,
                           CNPostalAddressPostalCodeKey
                         ];

        NSString* containerId = store.defaultContainerIdentifier;
        NSPredicate* predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];

        NSError* contactsError;
        NSArray* contacts = [store unifiedContactsMatchingPredicate:predicate
                                                        keysToFetch:keys
                                                              error:&contactsError];

        if (contactsError)
        {
            NSLog(@"Error fetching contacts %@", contactsError.debugDescription);
        }

        else
        {
            for (CNContact* contact in contacts)
            {
                NSString* firstName = contact.givenName;
                NSString* lastName = contact.familyName;

                NSLog(@"%@ %@:", firstName, lastName);

                for ( CNLabeledValue* lVal in contact.postalAddresses )
                {
                    // start with the assumption of a custom label
                    NSString* label = lVal.label;

                    if ( [CNLabelHome isEqualToString:label] )
                    {
                        label = @"Home";
                    }

                    else if ( [CNLabelWork isEqualToString:label] )
                    {
                        label = @"Work";
                    }

                    else if ( [CNLabelSchool isEqualToString:label] )
                    {
                        label = @"School";
                    }

                    else if ( [CNLabelOther isEqualToString:label] )
                    {
                        label = @"Other";
                    }

                    CNPostalAddress* address = (CNPostalAddress*)lVal.value;

                    NSLog(@"%@: [%@]", label, address.street);
                }
            }
        }
    }

    else
    {
        NSLog(@"Contact access NOT granted!");
    }
}];

以上示例仅基于您发布的代码,并简单地记录每个联系人的姓名,然后是为他们存储的每个(标记的)地址到控制台。在此之后您可以做任何您想做的事情,例如将它们添加到您自己的数组中或您希望进行的任何进一步处理。