0

I want to create one array from this one array that is 2d array

var array1 = [apples,oranges,grapes,vegetables,soups]
var arraylist = [[apples,oranges,grapes],[vegetables,soups]]
2
  • 1
    So, in the end you want to have the arraylist or the array1? Commented Oct 17, 2018 at 5:59
  • i want to create arraylist type of array which is divide by block like index 0 three elements and index 1 two elements Commented Oct 17, 2018 at 6:00

2 Answers 2

1

You can achieve this with simple loop though also, if you have only these 5 elements and you wants to separate them with code only.

var array1 = ["apples","oranges","grapes","vegetables","soups"]
var arraylist:[[String]] = [[String]]()

var innerArray: [String] = [String]()
var isFirstTime: Bool = true
for obj in array1 {
  if isFirstTime {
    innerArray.append(obj)
   if innerArray.count == 3 {
    arraylist.append(innerArray)
    innerArray.removeAll()
    isFirstTime = false 
  }
  } else {
    innerArray.append(obj)
    if innerArray.count == 2 {
    arraylist.append(innerArray)
    innerArray.removeAll()
  }
  }
}

print(arraylist)

Results: [["apples", "oranges", "grapes"], ["vegetables", "soups"]]

Sign up to request clarification or add additional context in comments.

1 Comment

it's helpful thank you for the the anwser @Bhavin Kansagara
0

What you have written is completely valid in Swift. Consider the following:

let apples = "apples"
let oranges = "oranges"
let grapes = "grapes"
let vegetables = "vegetables"
let soups = "soups"

var array1 = [apples, oranges, grapes, vegetables, soups]
var arraylist = [[apples, oranges, grapes], [vegetables, soups]]

Now adding types these two are:

var array1: [String] = [apples, oranges, grapes, vegetables, soups]
var arraylist: [[String]] = [[apples, oranges, grapes], [vegetables, soups]]

In general you can create groups which define how your array should be split. Check the following:

// Constants
let apples = "apples"
let oranges = "oranges"
let grapes = "grapes"
let vegetables = "vegetables"
let soups = "soups"

// Groups of items
let groups: [[String]] = {
    let fruits: [String] = [apples, oranges, grapes]
    let misc: [String] = [vegetables, soups]
    return [fruits, misc]
}()

// Array to seperate by group
let inputArray: [String] = [apples, oranges, grapes, vegetables, soups]

// Array of arrays of items per group. Outputs [["apples", "oranges", "grapes"], ["vegetables", "soups"]]
let grouppedArray: [[String]] = {
    return groups.compactMap { group in
        let filteredArray = inputArray.filter { group.contains($0) }
        return filteredArray.isEmpty ? nil : filteredArray
    }
}()

2 Comments

can i add 2d array in tableview section and in cell text i am creating expandable cell tableview by using this array list array so how could i add that array?
@ronit Sure. That is completely new question but number of sections is groupedArray .count, number of rows is groupedArray[section] .count and text in cell is cell.textLabel?.text = groupedArray[indexPath.section][indexPath.row]. Expandable need a bit more work but I am sure after you have the whole table view setup you will find answers to do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.