Thursday, February 21, 2019

Swift array declare

If you declare array like this.
var ar = [Int]()
Then add elements
ar[0]=1
ar[1]=2
When get values, it show you error, we must use append to add elements.
ar.append(1)
If you declare array like this.
var ar=[Int](count: 8, repeatedValue: 0)
Now array will have 8 elements with the same value 0.
If you add elements like this.
ar.append(1)
Now number 1 become the ninth elements, if get value from 1-8 it still show zero number.
To avoid this, we add element like this.
ar[0]=1
If you declare array like this.
var ar = NSMutableArray(capacity: 15)
Now we can’t use append, we must add elements like this
ar[0]=1

ar[1]=3

No comments:

Post a Comment