无法使用点符号swift访问函数成员

时间:2021-08-14 01:11:23

I cannot access the playerAv() function's elements outside of the function. How can I access it?

我无法在函数外部访问playerAv()函数的元素。我该如何访问它?

And also I tried to remove the function and use these lines outside of any function and I got an error in the second line. Why I couldn't assign audioLink value outside of a function?

而且我试图删除该函数并在任何函数之外使用这些行,我在第二行中出错。为什么我无法在函数外部分配audioLink值?

var audioLink = "http://www.liveatc.net/play/ltba.pls"
var player:AVPlayer = AVPlayer(URL: NSURL(string: audioLink))

I tried

let pa = playerAv()
pa.player.volume = currentValue //I got an error here:(
println(currentValue)

A part of code:

代码的一部分:

 func playerAv(){

        var audioLink = "http://www.liveatc.net/play/ltba.pls"
        var player:AVPlayer = AVPlayer(URL: NSURL(string: audioLink))
    }
@IBAction func sliderValueChanged(sender: UISlider) {
        var currentValue = Float(sender.value)
        println(currentValue)
        player.volume = currentValue
    }

Thanks

2 个解决方案

#1


You're very confused.

你很困惑。

You are mixing up classes and functions.

你正在混合类和功能。

A function's local variables are not accessible outside the function by design. They only exist inside the scope of the function, and cease to exist as soon as the function returns. Thus your playerAv() function doesn't do anything useful.

函数的局部变量在函数外部无法通过设计访问。它们只存在于函数的范围内,并在函数返回后立即停止存在。因此你的playerAv()函数没有做任何有用的事情。

It looks to me like you want playerAv to be a class, with instance variables:

在我看来,你希望playerAv成为一个具有实例变量的类:

class PlayerAv
{
  let audioLink: String = "http://www.liveatc.net/play/ltba.pls"
  var player: AVPlayer
  init()
  {
    player = AVPlayer(URL: NSURL(string: self.audioLink))
  }
}

Now you can create a PlayerAv object and use it:

现在您可以创建一个PlayerAv对象并使用它:

let pa = playerAv()
pa.player.volume = currentValue 

#2


If you declare var player inside the function, then this variable's scope is only inside the function.

如果在函数内部声明了var player,那么这个变量的作用域只在函数内部。

You need to put the Player creation inside a controller or any class that will suit you:

您需要将Player创建放在控制器或任何适合您的类中:

class MyPlayer {

    var player: AVPlayer

    init(link: String) {
        self.player = AVPlayer(URL: NSURL(string: link))
    }

}

let mp = MyPlayer(link: "http://www.liveatc.net/play/ltba.pls")
mp.player.volume = 2

#1


You're very confused.

你很困惑。

You are mixing up classes and functions.

你正在混合类和功能。

A function's local variables are not accessible outside the function by design. They only exist inside the scope of the function, and cease to exist as soon as the function returns. Thus your playerAv() function doesn't do anything useful.

函数的局部变量在函数外部无法通过设计访问。它们只存在于函数的范围内,并在函数返回后立即停止存在。因此你的playerAv()函数没有做任何有用的事情。

It looks to me like you want playerAv to be a class, with instance variables:

在我看来,你希望playerAv成为一个具有实例变量的类:

class PlayerAv
{
  let audioLink: String = "http://www.liveatc.net/play/ltba.pls"
  var player: AVPlayer
  init()
  {
    player = AVPlayer(URL: NSURL(string: self.audioLink))
  }
}

Now you can create a PlayerAv object and use it:

现在您可以创建一个PlayerAv对象并使用它:

let pa = playerAv()
pa.player.volume = currentValue 

#2


If you declare var player inside the function, then this variable's scope is only inside the function.

如果在函数内部声明了var player,那么这个变量的作用域只在函数内部。

You need to put the Player creation inside a controller or any class that will suit you:

您需要将Player创建放在控制器或任何适合您的类中:

class MyPlayer {

    var player: AVPlayer

    init(link: String) {
        self.player = AVPlayer(URL: NSURL(string: link))
    }

}

let mp = MyPlayer(link: "http://www.liveatc.net/play/ltba.pls")
mp.player.volume = 2