-1

Tell me what am I doing wrong? why is the error happening in my code? I can't figure it out, an error like this appears : not initialized at super.init call

enter image description here

I can't find any information, I will be grateful for any help, maybe it will be useful to someone in the future too

import AVKit
import SwiftUI


class UIVideoPlayer: UIView {
var videoURL : String
var playerLayer = AVPlayerLayer()

override init(frame: CGRect) {
    
    super.init(frame: frame)
    
    
    
    guard let url = URL(string: videoURL) else { return }

    let player = AVPlayer(url: url)
    //self.player = player
    player.isMuted = true
    player.play()
    
    NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: .main) { _ in
                    player.seek(to: CMTime.zero)
                    player.play()
        


    }
  
    playerLayer.player = player
    playerLayer.videoGravity = AVLayerVideoGravity(rawValue: AVLayerVideoGravity.resizeAspectFill.rawValue)
    
    layer.addSublayer(playerLayer)
}

override func layoutSubviews() {
    super.layoutSubviews()
    playerLayer.frame = bounds
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

struct PlayersView: UIViewRepresentable {

func makeUIView(context: Context) -> UIVideoPlayer {
    return UIVideoPlayer()
}

func updateUIView(_ uiView: UIVideoPlayer, context: Context) {
    
}
}
  • 1
    Does this answer your question? [Error in Swift class: Property not initialized at super.init call](https://stackoverflow.com/questions/24021093/error-in-swift-class-property-not-initialized-at-super-init-call) – pkamb Jun 17 '21 at 00:10
  • I do not understand how to implement this using my code example ( – Phantom Jun 17 '21 at 00:41
  • It's unclear what you want or expect. How is `videoURL` ever supposed to get set, if you don't set it? – matt Jun 17 '21 at 00:57
  • to download a lot of videos from modal – Phantom Jun 17 '21 at 01:27
  • You may _say_ that, but I do not _see_ you ever giving `videoURL` a value. You need to think about where this value is supposed to come from. Until you do, it is impossible to make sense of your code. – matt Jun 17 '21 at 01:35

1 Answers1

0

As you declared "videoURL" as store property

Initialization is the process of preparing an instance of a class, structure, or enumeration for use. This process involves setting an initial value for each stored property on that instance and performing any other setup or initialization that’s required before the new instance is ready for use.

https://docs.swift.org/swift-book/LanguageGuide/Initialization.html

so, you can solve this by using "Explicitly unwrapped Optional"

var videoURL : String!

Or you can declare videoURL as Optional

var videoURL : String?

Or assign initial value to "videoURL" before calling Super.init

override init(frame: CGRect) {
self.videoURL = "Your URL"
super.init(frame: frame)

Class initialization in Swift is a two-phase process. In the first phase, each stored property is assigned an initial value by the class that introduced it. Once the initial state for every stored property has been determined, the second phase begins, and each class is given the opportunity to customize its stored properties further before the new instance is considered ready for use.

https://docs.swift.org/swift-book/LanguageGuide/Initialization.html#ID216

Deepak J
  • 11
  • 1
  • thanks for your answer, but this is a single link, and I need to download a large number of links that I get from the model – Phantom Jun 17 '21 at 10:18
  • this is what I need to get in the end `struct ContentView: View { var video: Video var body: some View { PlayersView(videoURL: video.wp) } }` – Phantom Jun 17 '21 at 10:31
  • You can create your own Initializers : ' convenience init(with frame:CGRect ,videoUrls: [URL]) { //----- Perform your operation // Call designated self.init(frame: CGRect) }' – Deepak J Jun 17 '21 at 10:41
  • So, Now what issue are you facing? – Deepak J Jun 17 '21 at 10:49
  • It's hard for me to figure it out, I'm new to SWIFTUI, could you show it with my code example? I need not one video link, but many, I have a data model with different links, and I need to get video links from there – Phantom Jun 17 '21 at 10:50
  • Okay, after abstracting the URL what operation you are performing if you are not showing anything on view then we can create simple function which will return all abstract URLs. – Deepak J Jun 17 '21 at 11:00
  • I can somehow contact you to tell you more, I have a very large project, and I cannot place it here in full – Phantom Jun 17 '21 at 11:07