I have this class:
class MainView:UIView{
var categories:[Category]!
}
i want to set the categories arg, but i need to pass it by reference not value. because it's more efficient and better.
so if i did this:
let mainView = MainView()
mainView.categories = categoriesData.
then it pass it by value.
if i need to pass it by reference i could do that by using function inside the MainView()
class MainView:UIView{
var categories:[Category]!
fun setCategories(inout categories: Int){
self.categories = categories;
}
}
but if i don't want to use set function, How could i pass it by reference. e.g
mainView.categories = &categoriesData.
but that doesn't work ?thanks
Swift uses ARC (Automatic Reference Counting) when dealing with arrays, and it delays copying arrays until one of the copies is modified:
For example:
At step 1 above, there is only one copy of
[1, 2, 3, 4, 5]
in memory, anda
,b
, andc
are references to it.When
a
is modified in step 2, Swift givesa
and new copy of the array andb
andc
continue to reference the original array. So now there are 2 copies of the array in memory.Let's look at a much more involved example:
At step 1, the array of friends is created. It is referenced by the local variable
friends
.This reference goes away when
createFredsFriends()
returns, and the only reference to the array is held then by the local variablefriends
at step 2. Ownership of the array has been passed.At step 3, a second reference to the array has been assigned to the
friends
property offred
.At step 4,
createFred()
has returned, so the local variablefriends
is gone and no longer references the array. The only reference is in the property of thefred
object which is held by the variablefred
.So the array was created once, several references to it were created, and in the end there is a single reference to the array and all of this was done without a single copy operation.
Since Swift arrays are value types and copied when changed, you can't pass an array and then expect the original to be updated when the copy is. If you need that level of functionality, you can create a class wrapper for the array and then always access that array through the instance of that class.
Here I've modified the previous example to show how that would work: