如何通过引用传递没有功能的数组?迅速
我上了这个课:
class MainView:UIView{
var categories:[Category]!
}
我想设置类别arg,但是我需要通过引用而不是值来传递它.因为它更有效,更好.
i want to set the categories arg, but i need to pass it by reference not value. because it's more efficient and better.
所以,如果我这样做:
let mainView = MainView()
mainView.categories = categoriesData.
然后按值传递它.
如果我需要通过引用传递它,可以通过使用MainView()中的函数来做到这一点
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;
}
}
但是如果我不想使用set函数,如何通过引用传递它. 例如
but if i don't want to use set function, How could i pass it by reference. e.g
mainView.categories = &categoriesData.
但这不起作用吗?谢谢
mainView.categories = &categoriesData.
but that doesn't work ?thanks
Swift在处理数组时使用ARC(自动引用计数),并且将复制数组的操作延迟到修改其中一个副本之前:
Swift uses ARC (Automatic Reference Counting) when dealing with arrays, and it delays copying arrays until one of the copies is modified:
例如:
var a = [1, 2, 3, 4, 5]
let b = a
let c = a // 1
a.append(6) // 2
print(a.count)
print(b.count)
print(c.count)
在上面的步骤1中,内存中只有[1, 2, 3, 4, 5]
的一个副本,并且a
,b
和c
是对其的引用.
At step 1 above, there is only one copy of [1, 2, 3, 4, 5]
in memory, and a
, b
, and c
are references to it.
在步骤2中修改了a
后,Swift给出了a
和该数组的新副本,并且b
和c
继续引用了原始数组.因此,现在内存中有2个数组副本.
When a
is modified in step 2, Swift gives a
and new copy of the array and b
and c
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:
class Person: CustomStringConvertible {
let name: String
var friends: [Person] = []
init(name: String) {
self.name = name
}
var description: String { return name }
}
func createFredsFriends() -> [Person] {
let barney = Person(name: "Barney")
let wilma = Person(name: "Wilma")
let betty = Person(name: "Betty")
let friends = [barney, wilma, betty] // 1
return friends
}
func createFred() -> Person {
let fred = Person(name: "Fred")
let friends = createFredsFriends() // 2
fred.friends = friends // 3
return fred
}
let fred = createFred() // 4
print(fred.friends) // [Barney, Wilma, Betty]
-
在步骤1,创建朋友数组.它由局部变量
friends
引用.当
createFredsFriends()
返回时,该引用消失,然后在步骤2中由局部变量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.在步骤3中,已将对数组的第二个引用分配给
fred
的friends
属性.At step 3, a second reference to the array has been assigned to the
friends
property offred
.在步骤4,已返回
createFred()
,因此局部变量friends
消失了,不再引用该数组.唯一的引用位于变量fred
所保存的fred
对象的属性中.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.
由于Swift数组是值类型,并且在更改时会被复制,因此您不能传递数组,然后期望在复制时会更新原始数组.如果需要该级别的功能,则可以为该数组创建一个类包装器,然后始终通过该类的实例访问该数组.
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:
// Class to wrap array so that everyone references the same copy class FriendsWrapper { var friends: [Person] init(friends: [Person]) { self.friends = friends } } class Person: CustomStringConvertible { let name: String var friendsWrapper: FriendsWrapper? init(name: String) { self.name = name } func addFriend(friend: Person) { if let wrapper = friendsWrapper { wrapper.friends.append(friend) } else { friendsWrapper = FriendsWrapper(friends: [friend]) } } var description: String { return name } } func createFredsFriends() -> [Person] { let barney = Person(name: "Barney") let wilma = Person(name: "Wilma") let betty = Person(name: "Betty") let friends = [barney, wilma, betty] return friends } func createFred() -> Person { let fred = Person(name: "Fred") let friendsWrapper = FriendsWrapper(friends: createFredsFriends()) fred.friendsWrapper = friendsWrapper // Add friend to Fred object fred.addFriend(Person(name: "Bam Bam")) // Copy of array in local friendsWrapper is updated print(friendsWrapper.friends) // [Barney, Wilma, Betty, Bam Bam] return fred } let fred = createFred()