Forum Xamarin.iOS

How to loop through an array and add phone numbers to a contact

PeteOCPeteOC GBMember


The example I've got for adding a number is as follows which works:

var cellPhone = new CNLabeledValue<CNPhoneNumber>(CNLabelPhoneNumberKey.iPhone, new CNPhoneNumber("713-555-1212")); var workPhone = new CNLabeledValue<CNPhoneNumber>("Bob", new CNPhoneNumber("408-555-1212")); contact.PhoneNumbers = new CNLabeledValue<CNPhoneNumber>[] { cellPhone, workPhone };

I've got an array of phone number and type and I want to add the numbers like the example above, how would I implement this?

Thanks in advance,



Best Answer


  • PeteOCPeteOC GBMember

    Some additional information, I have deserialized some XML into an object so I have:

    newContact.phonenumbers.phonenumber[0].type and newContact.phonenumbers.phonenumber[0].number
    newContact.phonenumbers.phonenumber[1].type and newContact.phonenumbers.phonenumber[1].number

    etc etc

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai
    edited January 2017

    How about:

    for (int i = 0; i < newContact.phonenumbers.phonenumber.Count; i++) {
        var newPhone = new CNLabeledValue<CNPhoneNumber>(
            new CNPhoneNumber(newContact.phonenumbers.phonenumber[i].number));
        contact.PhoneNumbers = new CNLabeledValue<CNPhoneNumber>[] { newPhone };
Sign In or Register to comment.