【问题标题】:Error: Cannot convert value of type "[String]" to expected argument type "String?"错误:无法将类型“[String]”的值转换为预期的参数类型“String?”
【发布时间】:2023-04-05 23:28:01
【问题描述】:

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

   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)
    }
    }

}

【问题讨论】:

    标签: arrays swift string avaudioplayer


    【解决方案1】:

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

    let sounds = [ /* the contents of your array */ ]
    
    for s in sounds {
        // do stuff on s
    }
    

    【讨论】:

      【解决方案2】:

      Sounds 是一个类型字符串数组。

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

      您需要发送类似“Key1.wav”的字符串;不是整个数组

      【讨论】:

        【解决方案3】:

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

        在您的代码中,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 }

        希望帽子有所帮助!

        【讨论】:

          猜你喜欢
          • 2016-01-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-30
          • 1970-01-01
          • 1970-01-01
          • 2021-03-13
          • 2015-12-24
          相关资源
          最近更新 更多