简体   繁体   English

错误:无法将类型“ [String]”的值转换为预期的参数类型“ String?”

[英]Error: Cannot convert value of type “[String]” to expected argument type “String?”

Refer to this image please! 请参考这张图片! Help! 救命! Can anyone help me understand this error. 谁能帮助我了解此错误。 This is a creative project, in this particular function for my class, I am attempting to create an array of sounds (.wav format). 这是一个富有创造力的项目,在我班上的这一特定功能中,我尝试创建声音数组(.wav格式)。 Each sound that is being played corresponds to a letter from a txt file. 正在播放的每种声音都对应于txt文件中的一个字母。 Four different letters correspond to four different sounds. 四个不同的字母对应于四个不同的声音。 My program reads each letter from the txt file and identifies which sound to play. 我的程序从txt文件读取每个字母,并标识要播放的声音。 My goal is have the sounds overlap because when they are being played, they cut each other off. 我的目标是使声音重叠,因为在播放声音时,它们会相互切断。 In order to do this, it is to my understanding that I first need to create an array of sounds, add the array, make the audio player the deligate for each sound/letter it corresponds to, delete the audioPlayer in that array after it finishes playing, then add another array to the one that just finished playing, then remove the array after it finishes playing. 为了做到这一点,据我了解,我首先需要创建一个声音数组,添加该数组,使音频播放器对对应的每个声音/字母进行定位,在完成后删除该数组中的audioPlayer播放,然后将另一个数组添加到刚播放完的数组中,然后在播放完成后删除该数组。 I have to do this all while creating a string of sounds. 我必须在创建一串声音的同时做这一切。 I'm also having a hard time grasping the difference between an array and a string. 我也很难理解数组和字符串之间的区别。

   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 is an array of type strings. 声音是类型字符串的数组。

forResource is expecting a string; forResource需要一个字符串; not an array of type strings 不是类型字符串的数组

You need to send in a string like "Key1.wav"; 您需要输入“ Key1.wav”之类的字符串; not the entire array 不是整个数组

Should be simple. 应该很简单。 You need to iterate on the array and process each item separately. 您需要遍历数组并分别处理每个项目。

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

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

Arrays hold objects like Ints(whole numbers), Strings(text in Quotations), custom objects( users, cars, etc) 数组包含诸如整数(整数),字符串(引号中的文本),自定义对象(用户,汽车等)的对象

In your code, Sound is an array of Strings. 在您的代码中,Sound是一个字符串数组。 To access each string in sounds: 要访问声音中的每个字符串:

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 }

Hope hat helps! 希望帽子能帮到您!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取错误无法将类型&#39;((String,_)-&gt; String&#39;的值转换为预期的参数类型&#39;(_,String)-&gt; _&#39; - Getting error Cannot convert value of type '(String,_) -> String' to expected argument type '(_, String) -> _' 无法将类型“[String?]”的值转换为预期的参数类型“视频” - Cannot convert value of type '[String?]' to expected argument type 'Video' 无法将“String”类型的值转换为预期的参数类型“[Any]” - Cannot convert value of type 'String' to expected argument type '[Any]' Swift 错误无法转换“[String.Element]”类型的值(又名“Array<character> ') 到预期的参数类型 '[String]'</character> - Swift Error Cannot convert value of type '[String.Element]' (aka 'Array<Character>') to expected argument type '[String]' 尝试保存数组时出现错误-无法将类型[Data]的值转换为预期的参数类型&#39;[Dictionary <String, AnyObject> ]” - Trying to saveArray, got error - Cannot convert value of type '[Data]' to expected argument type '[Dictionary<String, AnyObject>]' 出现错误“无法将'listItem'类型的值转换为预期的参数类型'String?'” - Getting error "Cannot convert value of type 'listItem' to expected argument type 'String?'" 无法将&#39;Array [String]&#39;类型的值转换为预期的参数类型&#39;Set <String> &#39; - Cannot convert value of type 'Array[String]' to expected argument type 'Set<String>' 无法将“String”类型的值转换为预期的参数类型“String.Element”(又名“Character”) - Cannot convert value of type 'String' to expected argument type 'String.Element' (aka 'Character') Swift错误:无法将“ArraySlice”类型的值转换为预期的参数类型 - Swift Error: Cannot convert value of type 'ArraySlice' to expected argument type Swift:无法将&#39;String&#39;类型的值转换为预期的参数类型&#39;@noescape(AnyObject)引发-&gt; Bool&#39; - Swift: Cannot convert value of type 'String' to expected argument type '@noescape (AnyObject) throws -> Bool'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM