Xcode 14 / iOS 16 - UIViewRepresentables must be value types

I'm testing our app on the release candidate of Xcode 14 and while the app builds ok at runtime I get:

Fatal error: UIViewRepresentables must be value types: PlayerView

PlayerView is a class that handles an onload video - it has a published property and publishers that reference self (I'm quite new to iOS dev so haven't done much with combine) - it was obviously intended to be a class

The UIViewRepresentable protocol is handled like this:

extension PlayerView: UIViewRepresentable {

    public func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<PlayerView>) { }


    public func makeUIView(context: Context) -> UIView {

        return PlayerUIView(frame: .zero, player: player)

    }
}


private class PlayerUIView: UIView {

    private let playerLayer = AVPlayerLayer()

    init(frame: CGRect, player: AVPlayer) {

        super.init(frame: frame)

        playerLayer.player = player

        playerLayer.videoGravity = .resizeAspectFill

        layer.addSublayer(playerLayer)
    }

    required init?(coder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

    override func layoutSubviews() {

        super.layoutSubviews()

        playerLayer.frame = bounds

    }

}

Obviously this isn't the final release of Xcode 14 but does anyone know if this is a change in iOS 16 that means we'll have to make this a struct?

Thanks!

Were you able to use a class in earlier Xcode versions?

I am almost 100 percent sure you have to make PlayerView a struct. SwiftUI views are structs. The UIViewRepresentable protocol lets you wrap UIKit views to use them in SwiftUI apps, essentially converting UIKit views to SwiftUI views. Therefore views that use UIViewRepresentable must be structs.

Create a new class that has the @Published property and the Combine publishers. Add an instance of the class to PlayerView. That way you can use the @Published property and have the view be a struct.

Thanks for the reply!

This is the strange thing, it hasn’t been changed for a long time (added by the previous developer) and works fine with iOS 14 and 15. We’ve changed nothing in the app except trying to run with Xcode 14 and iOS 16

The particular class is in a swift package / spm

Thanks for your suggestion I will take a look 👍

Xcode 14 / iOS 16 - UIViewRepresentables must be value types
 
 
Q