Updating the data loading and model

Currently, the code in ViewController.swift specifies that just the given name, family name, image data, and image availability for a contact should be fetched. This needs to be expanded to the email address, postal address, and phone number are fetched as well. Update the retrieveContacts(store:) method with the following code for keysToFetch:

let keysToFetch = [CNContactGivenNameKey as CNKeyDescriptor,
                   CNContactFamilyNameKey as CNKeyDescriptor,
                   CNContactImageDataAvailableKey as CNKeyDescriptor,
                   CNContactImageDataKey as CNKeyDescriptor,
                   CNContactEmailAddressesKey as CNKeyDescriptor,
                   CNContactPhoneNumbersKey as CNKeyDescriptor,
                   CNContactPostalAddressesKey as CNKeyDescriptor]

Now that the code will fetch these new attributes for the contacts, the Contact class must be updated so it allows these new properties to be used. Add the following variable declarations to Contact.swift:

var emailAddress: String {
  // 1
  return String(contact.emailAddresses.first?.value ?? "--")
}

var phoneNumber: String {
  // 2
  return contact.phoneNumbers.first?.value.stringValue ?? "--"
}

var address: String {
  // 3
  let street = contact.postalAddresses.first?.value.street ?? "--"
  let city = contact.postalAddresses.first?.value.city ?? "--"

  return "(street) (city)"
}

Each of the three properties you have added is annotated with a comment. This is because these properties are not as straightforward to pass on as the others. Let's go over these comments one by one:

  • Because emailAddresses is an array and the value of an email address is an NSString? object instead of a String object, the first item in the list of email addresses is retrieved using optional chaining. If there is no first email address, the string -- is returned instead. This is then passed to the String initializer to convert the resulting NSString object to the String object that is expected to be returned. Being able to implement logic like this on the Contact model is convenient because it makes using the email address a lot more convenient for anybody who wants to display it.
  • Retrieving the string value for the phone number is a bit simpler. Optional chaining is used again to try and retrieve the first item in a list. If a phone number is found, the value's stringValue is used to turn the phone number into a String object. If no phone numbers are found, -- is returned here too.
  • The address is built by retrieving the first available address and extracting the street and city objects from it. These values are then combined in a string and returned. Again, missing values are replaced with --.

Now that the model is prepared, the time has come to pass the model to the contact details page so it can display real contact data.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset