我正在做一个在线音乐流媒体项目。
我有一个名为Song的对象数组 - 每首歌曲在该数组中都有来自SoundCloud的URL。
快速枚举每一首歌曲,然后调用SoundCloud Resolve API获取每首歌曲的直接流媒体URL。并将每个直接URL存储到数组中并加载到我的播放器中。
这似乎很容易,但#2步骤是异步的,因此每个直接URL可能会被存储到错误的数组索引中。我考虑使用Insert AtIndex而不是append,所以我在Playground中编写了示例代码,因为我的所有想法都无法成功地使直接URL的存储保持其顺序。
var myArray = [String?]()
func insertElementAtIndex(element: String?, index: Int) {
if myArray.count == 0 {
for _ in 0...index {
myArray.append("")
}
}
myArray.insert(element, atIndex: index)
}
insertElementAtIndex("HELLO", index: 2)
insertElementAtIndex("WORLD", index: 5)
我的想法在这个游乐场代码中,当然会产生一个错误,最后,我的问题是:如何正确使用这个insert atIndex?
Cannot convert value of type 'Int' to expected argument type 'String.Index'
。 - Awais Fayyaz