Initializer for conditional binding must have Optional type, not 'AnyView' – Swift

by
Ali Hasan
kingfisher swift-concurrency uikit xcode

The Problem:

In Xcode 14.3, we encountered an error while working with SwiftUI and Kingfisher. The error states ‘Initializer for conditional binding must have Optional type, not ‘AnyView” in the KFImageRenderer.swift file. Specifically, it occurs in a conditional binding statement where the expected type should be Optional, but the code currently uses ‘AnyView’ instead.

The Solutions:

Solution 2: Downgrading Kingfisher Version

If you are using Swift Package Manager, follow these steps:

  1. Open Package Dependencies.
  2. Double-click on the Kingfisher package.
  3. In the version field, change the version to 7.6.2.
  4. Click Done.

Solution 3: Upgrade Kingfisher Package

To resolve this issue, follow these steps to upgrade the Kingfisher package:

  • In Xcode, select the Package Navigator on the left sidebar.
  • Right-click on the Kingfisher package and select Upgrade to Latest Compatibility Version.
  • This will upgrade the Kingfisher package to a version that is compatible with Xcode 14.3 and resolves the error.

Solution 4: Verify Pod Configuration and Run pod Update

If you’re using Cocoa Pods for dependency management, ensure that your pod configuration is correct. Run the following command to update your pods and resolve any version conflicts:

pod update

Q&A

Outdated Kingfisher version?

Update Kingfisher to 7.6.1/7.6.2/update the package manually.

Using cocoa pods?

Run pod update.

Video Explanation:

The following video, titled "”iOS", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

”iOS