Swift 2D Array Initialisation with Objects - ios

I'm Trying to initialise a 2D array with objects in every cell.
I started with a class,
class cell {
var up = 1
var down = 1
var right = 1
var left = 1
}
And then Initialise my Array as such:
let row = Array<cell!>(count: 10, repeatedValue: cell())
let myArray = Array(count: 10, repeatedValue: row)
Now that works fine... Until I change the property of one of the objects.
myArray[0][0].left = 0
Then ALL objects in the Array have their "left" property set to 0.
How can I create objects that are independent of each other in the Array? (without using for-loops to append each item individually)

This won't work with Cell being a class because that is a reference type, so every cell in your array will be a reference to the same cell. If you can, change Cell to be a struct instead of a class. As a value type, every cell will be a unique copy.
If Cell must be a class, you could create myArray using nested maps, (which technically is still loops):
let myArray:[[Cell!]] = (1...10).map { _ in (1...10).map { _ in Cell() } }
Note, you should use names starting with an uppercase letter for class and struct names, which is why I change your cell to Cell.

It's because Cell is a class and classes are reference types. So the first line creates a row with 10 references to the same cell. The second line creates 10 unique rows (arrays are structs therefore value types) but all the unique rows have 10 references to the same cell.
There are several ways to resolve this. You can make sure you create 100 unique cells:
var array = [[Cell]]()
for _ in 0 ..< 10
{
var row = [Cell]()
for _ in 0 ..< 10
{
row.append(Cell())
}
array.append(row)
}
Or, you might consider making Cell a struct which is probably the best idea if a Cell is as simple as your example and you don't need inheritance.
struct Cell
{
var up = 1
var down = 1
var right = 1
var left = 1
}
In which case you can initialise the array the way you expect.
var array = [[Cell]](count: 10, repeatedValue: [Cell](count: 10, repeatedValue: Cell()))

Your class cell works is a reference type. What does that mean ?
Well, Apple's explanation about the topic is quite sufficient :
Types in Swift fall into one of two categories: first, “value types”,
where each instance keeps a unique copy of its data, usually defined
as a struct, enum, or tuple. The second, “reference types”, where
instances share a single copy of the data, and the type is usually
defined as a class. In this post we explore the merits of value and
reference types, and how to choose between them.
By declaring cell as a class, this is a reference type.
Refence type
// let's create an object
let cell1 = Cell()
// now we add a reference to this object
let cell2 = cell1
// now we change the top of cell1
cell1.up = 10
NSLog("\(cell2.up)")
// -> outputs : 10
// Since cell1 and cell2 represents the same object,
// you can't modify cell1 without modifying ce22.
What happen if you declares cell as a structinstead of a class? It becomes a value type :
Value type
// let's create an object
let cell1 = Cell()
// now we create a new object, cell2, with the value initialized
// with cell1's value. It's a copie, not a reference
let cell2 = cell1
// now we change the top of cell1
cell1.up = 10
NSLog("\(cell2.up)")
// -> outputs : 0
// Here cell1 and cell2 are totally different objects

What you are doing won't work. If you try to create an array and populate it repeatedValue and the repeated value is an object, the array contains multiple references to the same object. (Objects are a "reference type". When added to a container what gets added is a reference to the object, not a copy.)
You need to create a new object for each entry in your outer array:
(Edited to create an array of String objects since I don't have access to the OP's cell class)
typealias rowArray = [String!]
var myArray = [rowArray]()
for i in 1...10
{
myArray.append(rowArray(count: 10, repeatedValue: "Foo"))
}
Note that you're going to face the same problem with your inner array. You don't have an array of unique cell objects - you have an array that contains multiple references to a single cell object.

Related

Remove / delete selected cells from UICollectionView causes index out of bounds [sometimes]

I have a comments array declared as: var comments: [String] which I populate it with some Strings and I also have a UICollectionView within which I present the comments. My code is the following when I try to delete the selected cells from the UICollectionView:
if let indexPathsForSelectedItems = collectionView.indexPathsForSelectedItems {
for indexPath in indexPathsForSelectedItems {
comments.remove(at: indexPath.item) //I have only one section
}
collectionView.deleteItems(at: indexPathsForSelectedItems)
}
The issue is that sometimes when I delete the selected items, it creates an out of bounds exception on the comments array.
However when I use the following approach (create a copy array and replace the original one with its copy) no problem occurs:
var indexes: [Int] = []
for indexPath in indexPathsForSelectedItems {
indexes.append(indexPath.item)
}
var newComments: [String] = []
for (index, comment) in comments.enumerated() {
if !indexes.contains(index) {
newComments.append(comment)
}
}
comments = newComments
Why is this happening?
I am using Swift 3 and XCode 8.2.1
Sorting
If you're not sure that indexPathsForSelectedItems are sorted in descending order, and hence always deletes the highest index first, you will eventually run into an out of bounds. Deleting an item will change the indices for all array elements with higher indices.
You probably want to use indexPathsForSelectedItems.sorted(by: >).

How can I access an item from an array dynamically with Swift

I've got an Array that I'm using to populating the rows of a UITableView.
After a row is selected I need to retrieve information from the Array based on the row selected to populate some outlets (labels, textfields, etc.)
For example:
I create an itemSelected variable in the didSelectRowAtIndexPath in my ViewController for the TableView which I set to indexPath.row
itemSelected = indexPath.row
Then in my viewDidLoad for my otherViewController I need to retrieve the info by
array[itemSelected]
But, I get a compiler error that says: "Expression resolves to unused i-value"
In here you simply accessing the array but not calling any value. As a example if you have a key call "Name" in your array and you want to set it to a UILabel just do it as this.
self.Name.text = array[itemSelected].valueForKey("Name") as! String
if not just do something with it.
self.Name.text = array[itemSelected] as! String
OR
print(array[itemSelected])

Multidimensional Array Looping in cellForRowAtIndexPath Swift

I have a multidimensional array that I want to display the values of onto one UILabel in each respective cell.
My arrays look like this:
var arrayExample = [["beverages", "food", "suppliers"]["other stuff, "medicine"]]
I'm looping through these values in the cellForRowAtIndexPath in order for it to display on different cells (on a UILabel) the appropriate values:
if let onTheLabel: AnyObject = arrayOfContactsFound as? AnyObject {
for var i = 0; i < objects!.count; i++ {
cell?.contactsUserHas.text = "\(onTheLabel[indexPath.row][i])" as! String
print("arrayOfContactsFound Printing! \(onTheLabel)")
}
}
When printing to the console I get:
arrayOfContactsFound Printing! (
(
beverages,
"supply chain",
pharmacuticals
)
)
But on my label I get "beverages". That's it. How can I get the other 2 values (or X amount if there are more or less than 3 values)?
My for in loop is obviously not doing the trick. Assuming I can optimize/fix that to display all the values?
Thanks in advance.
In your loop you're setting the text of your label multiple times. Each time you set it it doesn't accumulate, it completely replaces the current text with the new text. You'll want something like this:
// Remove the cast and the loop in your code example, and replace with this
let items = arrayOfContactsFound[indexPath.row]
let itemsString = items.joinWithSeparator(" ")
cell?.contactsUserHas.text = itemsString
Another thing to note is your cast doesn't quite make a lot of sense.
var arrayExample = [["beverages", "food", "suppliers"]["other stuff, "medicine"]]
So arrayExample is of type [[String]]. I'm assuming each cell in your table view represents one of the arrays of strings in your array. So each cell represents one [String]. So your items should be arrayExample[indexPath.row]. The cast to AnyObject doesn't make too much sense. If anything you'd be casting it to [[AnyObject]], but there's no reason to because the compiler should already know it's [[String]].

"Cannot assign to" error iterating through array of struct

I have an array of structs:
struct CalendarDate {
var date: NSDate?
var selected = false
}
private var collectionData = [CalendarDate]()
Which I simply populate with a date like this:
for _ in 1...7 {
collectionData.append(CalendarDate(date: NSDate(), selected: false))
}
So when you tap on a collectionView, I simply want to loop through the data and mark them all as False.
for c in collectionData {
c.selected = false ///ERROR: Cannot assign to 'selected' in 'c'
}
Why do I get this error?
If I do this, it works fine but I want to know what I did wrong above:
for i in 0..<collectionData.count {
collectionData[i].selected = false
}
As I understand it, the iterator
for c in collectionData
returns copies of the items in collectionData - (structs are value types, not reference types, see http://www.objc.io/issue-16/swift-classes-vs-structs.html), whereas the iteration
for i in 0..<collectionData.count
accesses the actual values. If I am right in that, it is pointless to assign to the c returned from the iterator... it does not "point" at the original value, whereas the
collectionData[i].selected = false
in the iteration is the original value.
Some of the other commentators suggested
for (var c) in collectionData
but although this allows you to assign to c, it is still a copy, not a pointer to the original, and though you can modify c, collectionData remains untouched.
The answer is either A) use the iteration as you originally noted or B) change the data type to a class, rather than a struct.
because each 'c' is by default let, and this is a new instance of CalendarDate and the value of array at index copied to this for each step of for, and 'c' isn't pointer to the index of the array and it is just a copy of index, so if you set a new value to this, the new value does not apply in array.
but 'i' is used as index of array and can directly manipulate the values of array.
If you are using structs they are copies in the array. So even changing them only changes the copy, not an actual object in the array.
You have to make them a variable in the loop to be editable copy, and reassign them into the array right back.
If they are classes and not structs, than you don't have to reassign part, just do the var thing.
for (index, var c) in collectionData.enumerated() {
c.selected = false
collectionData[index] = c
}

trying to populate a cell label with a string from an array

So, I have a cell with one label inside. I am trying to populate that label text with the various items in my array - all strings.
My array
var restauranttypelist: [String] = ["American", "Asian", "Bakery & Deli",
"Burgers", "Italian", "Mexican", "Seafood", "Steakhouse"]
and my cell label text
let type = restauranttypelist [indexPath.row]
var typecell = tableView.dequeueReusableCellWithIdentifier("cellone") as RestaurantTypeCell
typecell.restaurantTypeLabel.text = restauranttypelist.text
return typecell
I have tried a number of solution ranging from ".text" seen above, to ".String", to "restauranttypelist:indexPath.row" to no avail.
I suppose I have two questions. Am I setting up my array correctly? Do I need to insert the "[String]" portion after the variable name?
Finally, how would I be able to set the cell label to the numerous items I have in my array?
Thanks for any help... beginning.
Jon
In let type = restauranttypelist[indexPath.row] you're accessing a String from your Array and storing it in type. Therefore, all you need is typecell.restaurantTypeLabel.text = type.
There's nothing wrong with how you setup the array. You don't need the [String] type annotation since it can be inferred from the value you are assigning to it, but having it there does no harm.
Finally, this doesn't affect how your code works, but it's nice to know anyway:
Swift variable names follow the convention of starting with a lowercase character, and then capitalizing every subsequent word. Following that convention your variable names should be typeCell and restaurantTypeList.

Resources