Swift For-In Loop with Enumerate on Custom Array2D Class

Swift for-in loop with enumerate on custom Array2D class?

It might suffice defining your own enumerate taking advantage of the one you already have:

func enumerate() -> AnyGenerator<((Int, Int), T?)> {
var index = 0
var g = array.generate()
return anyGenerator() {
if let item = g.next() {
let column = index % self.columns
let row = index / self.columns
++index
return ((column, row) , item)
}
return nil
}
}

Notice in this case you could avoid conforming to SequenceType since I use generate from the private array. Anyway it could be consistent to do so.

Here is how then you could use it:

var a2d = Array2D<Int>(columns: 2, rows: 4)
a2d[0,1] = 4

for ((column, row), item) in a2d.enumerate() {
print ("[\(column) : \(row)] = \(item)")
}

Hope this helps

iOS Swift: Getting repeated value while updating 2D Array in custom UITableView cell

You are adding to the nameArray and ageArray every time cellForRowAtIndexPath is called and you are not clearing them first. This seems inefficient and you should only populate those arrays when the input data changes, not when generating the cells.

I don't even think you need those arrays, as you could just do:

cell.nameLabel.text = "\(data[indexPath.row][0])"
cell.ageLabel.text = "\(data[indexPath.row][1])"

How can I create a 2D array in swift of a certain size with its values set to 0? Swift

You can use the Array.init(repeating:count:) initializer.

Example:

let row = Array(repeating: 0, count: 4) // [0, 0, 0, 0]

var table = Array(repeating: row, count: 4)

Result:

print(table)
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

How do I pass 2D array to the viewcontroller?

Declare a reference of next view controller (change the names of viewcontroller/storyboard/identifiers according to your requirements):

let nextVC = NextViewController()

or you can also instantiate from storyboard if you're using storyboards:

let nextVC = UIStoryboard(name: "Home", bundle: nil).instantiateViewController(withIdentifier: "NextViewController") as! NextViewController

now pass whatever values you want to the nextVC as 2D array:

let cities2DArray = [cityNameId, cityNames] as [Any]
nextVC.cities2DArray = cities2DArray

You can do all the above work inside prepare(for segue: UIStoryboardSegue, sender: Any?) if you're using segue with identifier, if not then simply do the work right before you're going to the nextVC programmatically.

Now, make sure you have these a 2DArray variables declared inside the NextViewController:

class NextViewController: UIViewController {

var cities2DArray = [Any]()

Updated:
In case you do want to make a tuple that has id and its corresponding name side by side in 1 array inside of tuple then you need to modify your for loop as following:

    var citiesTuple = [(id: Int, name: String)]()
for cityName in display.cities{
citiesTuple.append((id: cityName.id, name: cityName.name))
}

ObjC to Swift array loop , need help please

The first step is to convert your NSArray to have the proper type, in this case it looks like you want:

if let array = myArray as? [[String]] {
}

Then you have a number of options, the most effective of which is probably a nested iteration:

let myArray = NSArray(arrayLiteral:
NSArray(arrayLiteral: "150.10,310.20"),
NSArray(arrayLiteral: "400.10,310.20", "100,200")
)

if let source = myArray as? [[String]] {
for array in source {
for element in array {
print(element.componentsSeparatedByString(","))
}
}
}
// -> ["150.10", "310.20"]
// ["400.10", "310.20"]
// ["100", "200"]

If all you're wanting to do is extract the points, you can use map:

if let source = myArray as? [[String]] {
print(source.map {
$0.map { $0.componentsSeparatedByString(",") }
}
)
}
// -> [[["150.10", "310.20"]], [["400.10", "310.20"], ["100", "200"]]]

If you want to collapse the points into a single array, you can use flatMap, as pointed out by Helium, which will turn the array of arrays, into a simple array:

if let source = myArray as? [[String]] {
print(source.flatMap {
$0.map { $0.componentsSeparatedByString(",") }
}
)
}
// -> [["150.10", "310.20"], ["400.10", "310.20"], ["100", "200"]]

Of course, you can also extract it as [[Double]] instead of [[String]] by using yet another map call:

if let source = myArray as? [[String]] {
print(source.map {
$0.map { $0.componentsSeparatedByString(",").map { Double($0)! } }
})
}
// -> [[150.1, 310.2], [400.1, 310.2], [100.0, 200.0]]


Related Topics



Leave a reply



Submit