0

The code that used to work in iOS 9 was:

var valuesArray : [CNLabeledValue] = []

But I can't figure out how to do it in Swift 3.

pedrouan
  • 12,762
  • 3
  • 58
  • 74
Carlos Borges
  • 81
  • 1
  • 4
  • `CNLabeledValue` is a generic class in Swift 3, so you can't have something of type `CNLabeledValue` — it has to be `CNLabeledValue`. Which "Something" it is depends on what you're doing with this array, so there's not much more help we can provide without a bigger picture of how your array is used. – rickster Sep 23 '16 at 23:33
  • I'm working with the Contacts Framework, so is an array of CNPhoneNumber. – Carlos Borges Sep 24 '16 at 01:16
  • 'var valuesArray : [CNLabeledValue] = []' solve issues. – Chandan Sep 24 '16 at 06:00

2 Answers2

2

This is the solution:

var phoneNumbers : [CNLabeledValue<CNPhoneNumber>] = []

As OOPer pointed out in this post:

CNLabeledValue's generic parameter is declared as <ValueType : NSCopying, NSSecureCoding>. So, in this case, you can choose any type which conforms to NSCopying and NSSecureCoding. NSString does and String does not.

Community
  • 1
  • 1
Carlos Borges
  • 81
  • 1
  • 4
  • The referred sentence may lead some misunderstandings in your case. You may need to choose one appropriate type for `ValueType` -- in your case, it's `CNPhoneNumber`. The part "any type" does not apply to your issue. – OOPer Sep 24 '16 at 05:51
0

something like this (with example to fill out phone number):

            let phonesArray : [Phones] = phones!
            var phonesToAdd = [CNLabeledValue]()
            for phone in phonesArray
            {
                if let phoneT = phone.phoneType
                {
                    if phoneT.lowercaseString == "mobile"
                    {
                        let mobilePhone = CNLabeledValue(label: "mobile",value: CNPhoneNumber(stringValue: phone.phone))
                        phonesToAdd.append(mobilePhone)
                    }
                    if phoneT.lowercaseString == "landline"
                    {
                        let landlinePhone = CNLabeledValue(label: "landline",value: CNPhoneNumber(stringValue: phone.phone))
                        phonesToAdd.append(landlinePhone)
                    }
                }
            }
            contactData.phoneNumbers = phonesToAdd
nerowolfe
  • 4,787
  • 3
  • 20
  • 19