2 Replies
      Latest reply on Mar 27, 2020 2:43 PM by Claude31
      PyrixGreg Level 1 Level 1 (0 points)

        If you add the @Published to an array property, the willSet no longer fires. Remove the @Published and it works again.

        This worked in XCode 11.3.1. Can someone confirm the same behaviour? Is this not going to be supported in future versions?

         

        Test class

        public class FooWillSet

        {

            @Published var keys = ["front", "back"]

            {

                willSet

                {

                    print("new values\(newValue)")

                    print("old keys \(keys)")

                }

            }

        }

         

        func TestArray()

            {

                var foo = FooWillSet()

                foo.keys.append( "garage" )

                foo.keys.append( "condo" )

            }

        • Re: willSet broken for array properties in Xcode 11.4
          Claude31 Level 8 Level 8 (8,505 points)

          Yes, I confirm. Works in 11.3, not in 11.4 (in playground).

          Same with didSet.

           

          Should file a bug.

            • Re: willSet broken for array properties in Xcode 11.4
              Claude31 Level 8 Level 8 (8,505 points)

              The problem comes from append.

               

              I did the following test in playground, XCode 11.4, OSX 10.15.4

               

              public class FooWillSet
              {
                  @Published var keys = ["front", "back"]
                  {
                      willSet
                      {
                          print("new values\(newValue)")
                          print("old keys \(keys)")
                      }
                  }
              }
              
              func TestArray()
                  {
                      var foo = FooWillSet()
                      foo.keys.append( "garage" )
                      foo.keys.append( "condo" )
                      print("Called TestArray")
                  }
              TestArray()

               

              I get only

              Called TestArray

               

               

              Now, I add 2 new lines 18 and 19:

               

              public class FooWillSet
              {
                  @Published var keys = ["front", "back"]
                  {
                      willSet
                      {
                          print("new values\(newValue)")
                          print("old keys \(keys)")
                      }
                  }
              }
              
              func TestArray()
                  {
                      var foo = FooWillSet()
                      foo.keys.append( "garage" )
                      foo.keys.append( "condo" )
                      foo.keys = foo.keys + ["garage New"]
                      foo.keys = foo.keys + ["condo"]
                      print("Called TestArray")
                  }
              TestArray()

               

              I get:

              new values["front", "back", "garage", "condo", "garage New"]

              old keys ["front", "back", "garage", "condo"]

              new values["front", "back", "garage", "condo", "garage New", "condo"]

              old keys ["front", "back", "garage", "condo", "garage New"]

              Called TestArray

               

              Which shows the problem is that append is not triggering the observer (just as if it was working on a local copy ?)

              But adding directly and item does trigger the observer.

               

              Hope that will help focus bug search (did you file a bug report ?).

              I'll report a bug on my side.

               

              BUG REPORT : FB7642371