如何将json数据拆分为2个不同的数组?

时间:2017-02-26 22:01:05

标签: ios arrays swift

目前我们有一个数组,其中包含JSON对象的所有内容:

  var castArray: [CastData] = []

    CastData.updateAllData(urlExtension: "\(movieID)/credits", completionHandler: { results in

      guard let results = results else {
        print("There was an error retrieving upcoming movie data")
        return
      }
      self.castArray = results
})

我正在尝试将JSON对象的结果拆分为2个数组,前5个将进入第一个数组,其余部分将进入第2个数组:

var first5CastArrayObjects: [CastData]
var theRestofTheCastArrayObjects: [CastData] 

最好的方法是什么?

1 个答案:

答案 0 :(得分:4)

if castArray.count > 5 {
    let first5CastArrayObjects = castArray[0...4]
    var theRestofTheCastArrayObjects = castArray [5...castArray.count - 1]
} else {
    //Manage exception
}