gpt4 book ai didi

ios - "ViewController.Type does not have a member named ' 变量

转载 作者:行者123 更新时间:2023-12-03 02:36:06 25 4
gpt4 key购买 nike

我一直在 IOS 上使用 Swift 开发应用程序,当您按下按钮时它会发出声音(我是初学者)。
这段代码虽然一直给我带来问题,但当我刚刚输入“The Wilhelm 尖叫音效”时,该代码有效,但是当我尝试输入字符串时,它给了我“ViewController.Type 没有名为尖叫的成员”错误.

我已经坚持了一段时间,所以任何帮助将不胜感激。

class ViewController: UIViewController {

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


let scream: String = "The Wilhelm scream sound effect"



var pianoSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(scream, ofType: "mp3")!)
var audioPlayer = AVAudioPlayer()
}

最佳答案

你的代码在那里不起作用。你必须把它放在一个函数中,如下所示:

let audioPlayer = AVAudioPlayer()
let scream = "The Wilhelm scream sound effect"
func playMySound(sound:String) {
// you can get the file url instead of the path like this:
let soundUrl = NSBundle.mainBundle().URLForResource(sound, withExtension: "mp3")
// now you can play your audio using the soundUrl
// ...
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
playMySound("The Wilhelm scream sound effect")
// or
playMySound(scream)
}

关于ios - "ViewController.Type does not have a member named ' 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27726050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com