Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify the value of a CNLabeledValue, keeping the identifier unchanged

The new 'Contacts' framework introduces the CNLabeledValue class. This class is used to manage pairs label-value. But a CNLabeledValue has a third property: the identifier.

In the doc we can read: It is recommended that you use the identifier when searching for a previously known labeled value object in a re-fetched contact. The identifier can be persisted between the app launches.

So, if in the Contacts App the user changes the label, or the value, or both, it doesn't matter because we can use the identifier to identify the CNLabeledValue. The Contacts App modifies the CNLabeledValue, keeping the identifier unchanged.

I try to do the same but how can I modify the value for an existing fetched CNLabeledValue, keeping the identifier unchanged? For example, I want to change the "work" phone number. I received a CNLabeledValue for the current "work" phone number and now what?

For the CNContact, I create a mutableCopy

    CNMutableContact *mutableContact = [fetchedContact mutableCopy];

The mutableContact can be modified and it keeps the same contact identifier than the fetched contact.

The CNLabeledValue is an immutable value object and it does not have a mutable version. I also cannot give an identifier when I create a new CNLabeledValue.

Replace the CNLabeledValue with a new one with another identifier is not fair because my app is perhaps not the only one fetching from Contacts... and I will not appreciate another app modifies the CNLabeledValue identifiers...

Any idea?

like image 240
PatrickV Avatar asked Oct 16 '15 01:10

PatrickV


1 Answers

Just done some very quick research and looks like what you want to use is:

- (instancetype)labeledValueBySettingLabel:(NSString *)label value:(ValueType)value

https://developer.apple.com/library/watchos/documentation/Contacts/Reference/CNLabeledValue_Class/index.html#//apple_ref/occ/instm/CNLabeledValue/labeledValueBySettingLabel:value:

From the docs: Returns a labeled value object with the specified label and value with the existing identifier.

like image 111
simonthumper Avatar answered Nov 12 '22 10:11

simonthumper