This seems so simple but I'm stuck. I'm getting the usual Index Out Of Bounds Swift errors. It seems Java can set the amount of indexes an array has from the start and increment the total number of indexes as well.
I know what the issue is but I don't know the Swift equivalent of this Java function. The Java function has a post incrementor for increasing the index count of the EMPTY array. I don't know how to write that in Swift. With Swift you have to use append. You can not use subscripting on an empty array. PLUS I don't know how to increase the index count.
How do I convert this Java to Swift?
Java
private int[] theArray;
private int itemsInArray = 0;
public void addItemToArray(int newItem) {
theArray[itemsInArray++] = newItem;
}
Swift
var theArray = [Int]()
var itemsInArray = 0
func addItemArray(newItem: Int) {
theArray[itemsInArray] += newItem
}
addItemArray(newItem: 5)
itemsInArray? You could just usetheArray.append(newItem)and then simply usetheArray.countto get the number of available elements. A Swift Array is more like aListthen a Java arrayitemsInArraycould be higher then the available elements (ielength) it could generate aIndexOutOfBoundsExceptionas well...and the Swift code isn't even close to doing the same thing