Home > Net >  Swift error: 'override' can only be specified on class members
Swift error: 'override' can only be specified on class members

Time:06-24

I need to use Platform View to display the native video player AVPlayer for ios. When I go to the VideoPlayerPlatformView page I get a black screen on the simulator. How do I properly add viewDidLoad() to my code?

import UIKit
import Flutter
import AVKit
import AVFoundation

public class VideoView: NSObject, FlutterPlatformView {
    let frame: CGRect
    var _view: UIView
    init(
         frame: CGRect,
         viewIdentifier viewId: Int64,
         arguments args: Any?,
         binaryMessenger messenger: FlutterBinaryMessenger?
    ) {

            _view = UIView()
            self.frame = frame
            super.init()
             createNativeView(view: _view)
        }
    public func view() -> UIView {
            return _view
        }

    func createNativeView(view _view: UIView){
        override func viewDidLoad() {
            super.viewDidLoad()
            preparePlayer()
        }
      }
    private func preparePlayer() {
        let url = URL(string: "link")
        let player = AVPlayer(url: url!)
        let playerLayer = AVPlayerLayer(player: player)
        playerLayer.frame = self._view.bounds
        self._view.layer.addSublayer(playerLayer)
        player.play()
    }
}

CodePudding user response:

You have accidentally nested a function inside another function:

func createNativeView(view _view: UIView){
    override func viewDidLoad() {
        super.viewDidLoad()
        preparePlayer()
    }
}

What you like want instead is:

override func viewDidLoad() {
    super.viewDidLoad()
    preparePlayer()
}

But it will still report errors, since you are deriving from NSObject, and that class doesn't have a function viewDidLoad that you could override. A UIViewController or NSViewController does, though.

In your original code, you created a function createNativeView. And inside that, you created a new function viewDidLoad. You are allowed to do that, but this inner function is totally unrelated to the function of the same name that might exist on a class.

  • Related