I have been playing around with CNContactFormatter. I went through the documentation at Apple's official website for the documentation for this. CNContactFormatter doesn't do much. Is there a way for me to use this formatter to produce a string that is dependent on what fields the contact has values for?

For instance, I would like for the formatter to create a string that consists of the family name, comma, then given name, space, and the nickname in quotation marks, if the contact has those fields filled in, but if the contact is missing one of those fields, then the string consists only the names that are provided, formatted accordingly, so that there is not a comma out of place.

This formatter doesn't seem to even allow me to set the name order. This doesn't seem to be very useful in that way. I do understand the usefulness of this formatter in displaying the name of the contact according to the language or locality setting on the device.

I believe I've probably learned as much as I can about CNContactFormatter, but I'm hoping that there is something someone knows that can help me.

I put my code below that I used to play around with CNContactFormatter. I show only the code for the fetchMe() function that is necessary to understand what it does.

func fetchMe() -> CNContact? {
    
}


func tryContactFormatter() {
    
    guard let me = fetchMe() else {
        
        print("me was not found")
        
        return
        
    }
    
    guard let contactFormatterStringWhenStyleNotChanged = contactFormatter.string(from: me) else {
        
        print("contactFormatter.string() returned nil")
        
        return
        
    }
    
    print("contactFormatterStringWhenStyleNotChanged: \(contactFormatterStringWhenStyleNotChanged)")
    
    guard let cnContactFormatterString = CNContactFormatter.string(from: me, style: .fullName) else {
        
        print("cnContactFormatter.string() returned nil")
        return
        
    }
    
    print("cnContactFormatterString: \(cnContactFormatterString)")
    
    print("delimiter for me: \(String(describing: CNContactFormatter.delimiter(for: me)))")
    
    print("name order raw value for me: \(CNContactFormatter.nameOrder(for: me).rawValue)")
    
    print(CNContactFormatter.descriptorForRequiredKeysForDelimiter)
    
    print(CNContactFormatter.descriptorForRequiredKeysForNameOrder)
    
    print(CNContactFormatter.descriptorForRequiredKeys(for: .fullName))

    print(CNContactFormatter.descriptorForRequiredKeys(for: .phoneticFullName))

    print("formatter style raw value: \(contactFormatter.style.rawValue)")
    
    contactFormatter.style = .phoneticFullName
    
    print("formatter style raw value: \(contactFormatter.style.rawValue)")
    
    guard let contactFormatterStringAfterStyleHasBeenChanged = contactFormatter.string(from: me) else {
        
        print("contactFormatter.string() returned nil")
        
        return
        
    }
    
    print("contactFormatterStringAfterStyleHasBeenChanged: \(contactFormatterStringAfterStyleHasBeenChanged)")
    
}
0

There are 0 best solutions below