无法使用类型为“([(ModelMessageBridge)], atindex: int)”的参数列表调用 'insert'

Cannot invoke 'insert' with an argument list of type '([(ModelMessageBridge)], atindex: int)'

我试图在一个数组前加上另一个消息数组,但出现错误:

Cannot invoke 'insert' with an argument list of type '([(ModelMessageBridge)], atindex: int)'

这是我的代码:

var JSQmessages = [ModelMessageBridge]()
//fill up the array here

self.collectionView.performBatchUpdates({

   // indexPaths for earlier messages
   let lastIdx = history.count - 1
   var indexPaths: [AnyObject] = []
   for i in 0...lastIdx {
             indexPaths.append(NSIndexPath(forItem: i, inSection: 0))
   }

   //Convert the new messages in modelbridge
   var messages = [ModelMessageBridge]()

   for message in history {
      messages.append(ModelMessageBridge(message: message))
   }

   // insert messages and update data source
   self.collectionView.insertItemsAtIndexPaths(indexPaths)
   self.JSQmessages.insert(messages, atIndex: 0) //error here

两个数组的类型相同,所以我不明白为什么它不起作用...

您可以使用 splice 在给定索引处插入新元素:

self.JSQmessages.splice(messages, atIndex: 0)

此外,您可以使用 map:

大大简化您的代码
self.collectionView.performBatchUpdates({
    let indexPaths = (0..<history.count).map {
        NSIndexPath(forItem: [=11=], inSection: 0)
    }

    let messages = history.map {
        ModelMessageBridge(message: [=11=])
    }

    self.JSQmessages.splice(messages, atIndex: 0)
    self.collectionView.insertItemsAtIndexPaths(indexPaths)