-4

I want to get together every 8 item in array and create 1 item (I want to get 0...8, 9,...17 etc and make these one string.) Let me say more clear I have this array :

["ALINMA Bostancı - Yan Yol - Yan apt no:4 - Kadıköy - 7 - 23 - 29.103910631539957 - 40.96400403065958", "TESLIM Bostancı - Yan Yol - Yan apt no:4 - Kadıköy - 7 - 23 - 29.103910631539957 - 40.96400403065958", "10:00 - 11:00", "19/11/2019", "18:00 - 19:00", "21/11/2019", "Tshirt = 4", "Testçi", "ALINMA Bostancı - Yan Yol - Yan apt no:4 - Kadıköy - 7 - 23 - 29.103910631539957 - 40.96400403065958", "TESLIM Bostancı - Yan Yol - Yan apt no:4 - Kadıköy - 7 - 23 - 29.103910631539957 - 40.96400403065958", "18:00 - 19:00", "22/11/2019", "", "27/11/2019", "Tshirt = 4 ; Sweatshirt = 3", "Testçi", "ALINMA 19 Mayıs - Bayar Caddesi - Knkk  - Kadıköy - Kjb - Bng - 29.088674699528724 - 40.975391102375454", "TESLIM 19 Mayıs - Mehpare Sokak - Dergah no:1  - Kadıköy - 4 - 14 - 29.088822539423138 - 40.97780621764736", "18:00 - 19:00", "20/11/2019", "10:00 - 11:00", "23/11/2019", "Tshirt = 1 ; Kaz tüyü mont = 2", "Mehmett"]

I want to make it like

["ALINMA Bostancı - Yan Yol - Yan apt no:4 - Kadıköy - 7 - 23 - 29.103910631539957 - 40.96400403065958 TESLIM Bostancı - Yan Yol - Yan apt no:4 - Kadıköy - 7 - 23 - 29.103910631539957 - 40.96400403065958  10:00 - 11:00  19/11/2019  18:00 - 19:00 21/11/2019 Tshirt = 4  Testçi", "ALINMA Bostancı - Yan Yol - Yan apt no:4 - Kadıköy - 7 - 23 - 29.103910631539957 - 40.96400403065958 TESLIM Bostancı - Yan Yol - Yan apt no:4 - Kadıköy - 7 - 23 - 29.103910631539957 - 40.96400403065958 18:00 - 19:00 22/11/2019   27/11/2019 Tshirt = 4 ; Sweatshirt = 3  Testçi", "ALINMA 19 Mayıs - Bayar Caddesi - Knkk  - Kadıköy - Kjb - Bng - 29.088674699528724 - 40.975391102375454 TESLIM 19 Mayıs - Mehpare Sokak - Dergah no:1  - Kadıköy - 4 - 14 - 29.088822539423138 - 40.97780621764736  18:00 - 19:00  20/11/2019  10:00 - 11:00  23/11/2019  Tshirt = 1 ; Kaz tüyü mont = 2  Mehmett"]

I have already tried joined() method but How can I give range of 8 element? Can you help me?

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
swifty2
  • 101
  • 9

2 Answers2

2

Using this little extension (can be found here):

extension Collection where Index == Int {
    func chunked(by chunkSize: Int) -> [[Element]] {
        stride(from: startIndex, to: endIndex, by: chunkSize).map { Array(self[$0..<Swift.min($0 + chunkSize, count)]) }
    }
}

You can split your array into chunks of any size:

x.chunked(by: 8)

Then you can join each chunk:

x.chunked(by: 8).map{ $0.joined(separator: " ") }

x is used to demonstrate: let x = (1...80).map{ String($0) } but you should replace it with the actual array

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
0

You can take a part of the array and join it to string.

let datas = [YOUR_CURRENT_DATAS]
let firstItem = Array(datas[0..<8]).joined("")
let secondItem = Array(datas[8..<16]).joined("")
... 
// For dynamic datas you can loop the datas array
var newDatas: [String] = []
var i = 0
while (i < datas.count) {
    let item = Array(datas[i..<(i+8)]).joined(separator: "")
    newDatas.append(item)
    i += 8
}

Ayazmon
  • 1,360
  • 8
  • 17
  • Thanks for your answer but my array counts will be dynamic so it will be change. I need some range operations like range . How can I solve it? – swifty2 Nov 21 '19 at 09:42