Error: Cannot convert value of type "[String]" to expected argument type "String?"

Error: Cannot convert value of type "[String]" to expected argument type "String?"

Refer to this image please!求助!谁能帮我理解这个错误。这是一个创意项目,在我 class 的这个特定功能中,我试图创建一个声音数组(.wav 格式)。正在播放的每个声音都对应于 txt 文件中的一个字母。四个不同的字母对应四种不同的发音。我的程序从 txt 文件中读取每个字母并确定要播放的声音。我的目标是让声音重叠,因为当它们被播放时,它们会相互切断。为了做到这一点,据我了解,我首先需要创建一个声音数组,添加该数组,使音频播放器成为它对应的每个 sound/letter 的代理,然后删除该数组中的音频播放器播放完了,然后在刚刚播放完的数组上再添加一个数组,播放完再把数组移除。我必须在创建一串声音的同时完成所有这些工作。我也很难理解数组和字符串之间的区别。

   func playSound() {

    let sound: [String] = ["Keys1.wav", "Keys2.wav", "Keys3.wav", "Keys4.wav"]


    if let audioPath = Bundle.main.path(forResource: "Keys1", ofType: "wav") {
    do {
        sound = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: audioPath!))
        activeSound.delegate = self
        Swift.print("Audio was loaded")

    }
    catch {
        Swift.print("Can't read audio file")
        debug(error.localizedDescription)
    }
    }

}

Sounds 是一个字符串数组。

forResource 需要一个字符串;不是字符串类型的数组

您需要发送一个像"Key1.wav"这样的字符串;不是整个数组

应该很简单。您需要迭代数组并分别处理每个项目。

let sounds = [ /* the contents of your array */ ]

for s in sounds {
    // do stuff on s
}

数组包含整数(整数)、字符串(引号中的文本)、自定义对象(用户、汽车等)等对象

在您的代码中,Sound 是一个字符串数组。要访问声音中的每个字符串:

for string in sound { / perform actions on the individual sound here / your do catch block would go here, so it would get performed for each string }

希望帽子能帮到你!