skip to Main Content

I am apptemting to learn SwiftUI, Swift, and Xcode for macOS. The code below works but I’d like to have no warnings. The complete warning is:

‘onChange(of:perform:)’ was deprecated in macOS 14.0: Use onChange with a two or zero parameter action closure instead.

but I cannot interpret it. I’m using macOS 15

This works but the warning is generated.

.onChange(of: thisImageIndex) { newValue in
    // Update currentDatum when the index changes
    if newValue >= 0 && newValue < imageList.count {
        currentDatum = imageList[newValue]
        imageCaption = currentDatum!.displayName
    }
}

2

Answers


  1. Chosen as BEST ANSWER

    I figured it out for my purpose! I guess I'm getting better at this!

    .onChange(of: thisImageIndex) {
        if thisImageIndex >= 0 && thisImageIndex < imageList.count {
          currentDatum = imageList[thisImageIndex]
          imageCaption = currentDatum!.displayName
      }
    }
    

  2. To remove warning, you need to update your code to add old value

    .onChange(of: thisImageIndex) { oldValue, newValue in
        // Update currentDatum when the index changes
        if newValue >= 0 && newValue < imageList.count {
            currentDatum = imageList[newValue]
            imageCaption = currentDatum!.displayName
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search