Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 调用TableViewCell中的函数后,变量变为nil_Swift_String_Uitableview_Url_Avplayer - Fatal编程技术网

Swift 调用TableViewCell中的函数后,变量变为nil

Swift 调用TableViewCell中的函数后,变量变为nil,swift,string,uitableview,url,avplayer,Swift,String,Uitableview,Url,Avplayer,我试图将URL传递给UITableViewCell类中的多个函数。在@objc函数(用于处理按钮触摸)中,字符串变量(videoString)可以完美地工作。常规函数在展开字符串时会一直查找nil 我已经尝试过调整可选的展开(?/!)。无论我做什么,函数都会找到nil。为了测试这一点,我将@objc添加到有问题的函数中,当我单击connected按钮时,它实际上起了作用 var videoString = String() 这一个有效: var parent:Home? @objc func

我试图将URL传递给UITableViewCell类中的多个函数。在@objc函数(用于处理按钮触摸)中,字符串变量(videoString)可以完美地工作。常规函数在展开字符串时会一直查找nil

我已经尝试过调整可选的展开(?/!)。无论我做什么,函数都会找到nil。为了测试这一点,我将@objc添加到有问题的函数中,当我单击connected按钮时,它实际上起了作用

var videoString = String()
这一个有效:

var parent:Home?
@objc func shareSheet() {
    let items = [URL(string: videoString)!]
    let ac = UIActivityViewController(activityItems: items, 
    applicationActivities: nil)
    parent?.present(ac, animated: true)
}
这也是:

@objc func setupPlayerView() {
    let videoURL = videoString
    if let url = URL(string: videoURL) {
        player = AVPlayer(url: url as URL)
        let playerLayer = AVPlayerLayer(player: player)
        playerLayer.frame = videoFrame.bounds
        videoFrame.layer.addSublayer(playerLayer)
        player?.play()
    } else {
        print("Invalid")
    }
}
这个不

func setupPlayerView() {
    let videoURL = videoString
    if let url = URL(string: videoURL) {
        player = AVPlayer(url: url as URL)
        let playerLayer = AVPlayerLayer(player: player)
        playerLayer.frame = videoFrame.bounds
        videoFrame.layer.addSublayer(playerLayer)
        player?.play()
    } else {
        print("Invalid")
    }
}
单元格样式:

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    setupPlayerView()
    addSubview(videoFrame)
    addSubview(share)

    share.addTarget(self, action: #selector(shareSheet), for: .touchUpInside)

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

我希望setupPlayerView()接受videoString,但它总是发现nil并打印“无效”。我认为这与闭包有关,因为另一个函数正确地处理了闭包。

UPDATE

我在cellForRowAt的TableView文件中声明URL并将其传递给AVPlayer,从而使其正常工作:

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) 
-> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! EpisodeCell
    let currentLastItem = episodes[indexPath.row]
    cell.episode = currentLastItem
    let videoURL = currentLastItem.videoString
    let postURL = URL(string: videoURL)
    cell.videoPlayerItem = AVPlayerItem.init(url: postURL!)
    return cell
}
然后转到TableViewCell内的函数:

    func setupPlayerView() {
        self.player = AVPlayer.init(playerItem: self.videoPlayerItem)
        let playerLayer = AVPlayerLayer(player: player)
        playerLayer.frame = videoFrame.bounds
        videoFrame.layer.addSublayer(playerLayer)
        player?.play()
    }

您正在从单元格初始值设定项调用
setupPlayerView()
,此时,
videoString
为nil/空。您正在其他函数中获得值,因为在调用这些函数之前必须先设置
videoString