2 Replies
      Latest reply: Nov 4, 2016 6:27 AM by contactnkm RSS
      glaullon Level 1 Level 1 (0 points)

        Just upgrade to Swift 3. After open and convert my codes I found lots of errors. One of them is about CNLabeledValue.

         

        With Swift 2.3, it works well with:

         

        var toExcludeOrCopy : CNLabeledValue?
        

         

        With Swift 3, it gives me the error message:

        Reference to generic type 'CNLabeledValue' requires arguments in <...>.

        After I fixed it with XCode, it changed to:

         

        var toExcludeOrCopy : CNLabeledValue<AnyObject>?
        

         

        However, Xcode gives me error message with this fix:

        Type 'AnyObject' does not conform to protocol 'NSCopying'.

        I know the error is because of the generic type but I don't know how to declare with CNLabeledValue. My purpose is to store any the CNLabeledValue including phone number, email, address etc to one var.

        Any help will be appreciate.

        • Re: CNLabeledValue var
          eskimo Apple Staff Apple Staff (7,005 points)

          Oh, that’s not much fun.  Normally I’d do this like this:

          var toExcludeOrCopy : CNLabeledValue<NSCopying>? = nil
          

          but that doesn’t work because CNLabeledValue’s ValueType has to conform to both NSCopying and NSSecureCoding.  And you can’t do this:

          var toExcludeOrCopy : CNLabeledValue<NSCopying & NSSecureCoding>? = nil
          

          because NSSecureCoding has static requirements.  You can do this:

          class MyClass<ValueType> where ValueType : NSCopying, ValueType : NSSecureCoding {
              func myFunction()  {
                  var toExcludeOrCopy : CNLabeledValue<ValueType>? = nil
                  …
              }
          }
          

          but it’s probably not what you’re looking for (the client would have to tell MyClass what type it’s working on).

          If no one else chimes in, I’m going to recommend that you bounce this over to swift-users to see if anyone over there has any suggestions.

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"

            • Re: CNLabeledValue var
              contactnkm Level 1 Level 1 (0 points)

              Similar Problem but cannot get this to work.

               

              Worked perfectly in 2.2 Swift but fails miserably in swift 3.0.

              Spent days trying to work this out to no avail.

               

              Original code 2.2 (working)

               

              function call is from

               

              let editableContact = contact.mutableCopy() as! CNMutableContact

              editableContact.contactRelations.append( relation(key, arbitraryValue: value))    <<------- calls relation here with 2 string args

               

              func relation(arbitraryKey: String, arbitraryValue: String) -> CNLabeledValue      <<------- Creates a CNContactRelation and adds it to the CNLabeledValue

              {

                let myValue = CNContactRelation(name: arbitraryValue)

                let myKeyValue = CNLabeledValue(label: arbitraryKey, value: myValue)

                return myKeyValue

              }

               

              Above worked fine!!

               

              Now I tried to get this working in 3.0 with the last combination ( tried many before all failed )

               

              func relation(_ arbitraryKey: String, arbitraryValue: String) -> CNLabeledValue<ValueType>

              {

                      let myKeyValue = CNLabeledValue(label: arbitraryKey, value: arbitraryValue as NSString)

                      return myKeyValue

              }

              But get errors relating to generics