斯威夫特 - 麻烦来自追加数组类 [英] Swift - trouble appending array from class
问题描述
我无法理解为什么以下code与最新信息替换所有的数组元素,当我试图只是其追加。
I am having trouble understanding why the following code is replacing all of my array elements with the latest information when I am trying to just append it.
我有以下类:
class usersAvailable {
var username = String()
var userAvatar = UIImage()
}
我定义为使用类以下变量:
I have the following variables defined to use the class:
//the array which will hold users available to follow
var usersAvailableArray = [usersAvailable]()
var aUser = usersAvailable()
要检查我的理智我也已经宣布国家进入常规阵,看看我得到正确的价值观:
to check my sanity I also have declared a 'regular' array to see if I am getting the right values:
var usersArray:[[String]] = [["",""]]
当我执行以下code:
when i execute the following code:
func getAllUsers() {
usersAvailableArray.removeAll(keepCapacity: true)
usersArray.removeAll(keepCapacity: true)
var query = PFQuery(className: "_User")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
for object in objects! {
self.aUser.username = object["username"] as! String
println("aUser.username before append: \(self.aUser.username)")
println(self.aUser)
self.usersAvailableArray.append(self.aUser)
self.usersArray.append([self.aUser.username])
println("first element of usersAvailableArray: \(self.usersAvailableArray.first?.username)")
println("last element of usersAvailableArray: \(self.usersAvailableArray.last?.username)")
println("number of elements currently in usersAvailableArray: \(self.usersAvailableArray.count)")
println("first element of usersArray: \(self.usersArray.first)")
println("last element of usersArray: \(self.usersArray.last)")
println("number of elements currently in usersArray: \(self.usersArray.count)")
}
这里是控制台输出我得到:
here is the console output I get:
aUser.username before append: joeBob
ParseStarterProject.usersAvailable
first element of usersAvailableArray: Optional("joeBob")
last element of usersAvailableArray: Optional("joeBob")
number of elements currently in usersAvailableArray: 1
first element of usersArray: Optional(["joeBob"])
last element of usersArray: Optional(["joeBob"])
number of elements currently in usersArray: 1
aUser.username before append: monkey
ParseStarterProject.usersAvailable
first element of usersAvailableArray: Optional("monkey")
last element of usersAvailableArray: Optional("monkey")
number of elements currently in usersAvailableArray: 2
first element of usersArray: Optional(["joeBob"])
last element of usersArray: Optional(["monkey"])
number of elements currently in usersArray: 2
aUser.username before append: support
ParseStarterProject.usersAvailable
first element of usersAvailableArray: Optional("support")
last element of usersAvailableArray: Optional("support")
number of elements currently in usersAvailableArray: 3
first element of usersArray: Optional(["joeBob"])
last element of usersArray: Optional(["support"])
number of elements currently in usersArray: 3
为什么我的usersAvailableArray更换阵列UsersAvailableArray中的每个元素的追加?
Why is my append of usersAvailableArray replacing every element in the array UsersAvailableArray?
推荐答案
正在传递数据通过引用数组中,并在每个循环中,您更改参考值:
You are passing data to the array by reference, and in every loop you change the value of the reference:
self.aUser.username = object["username"] as! String
self.usersAvailableArray.append(self.aUser)
试着每次创建新用户:
Try to create a new user every time:
var newUser = usersAvailable()
newUser.username = object["username"] as! String
self.usersAvailableArray.append(newUser)
这是发生因为对象是你从追加一个类来,从<一个href=\"https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html\"相对=nofollow> APPE文档:
This is happening because the object you are append come from a class, from appe documentation:
类是引用类型
与值类型,引用类型而不是当他们被复制
分配给一个变量或常数,或当它们被传递给
功能。而不是副本,以相同的参考现有
实例代替。你可以看到更多的价值和引用
在斯威夫特
Unlike value types, reference types are not copied when they are assigned to a variable or constant, or when they are passed to a function. Rather than a copy, a reference to the same existing instance is used instead. You can see more about values and references in Swift
另一个解决方案是你的类变成一个结构,如下图所示,结构不同,如果类使得当它被传递,而不是通过一个参考值的副本。
Another solution is to change your class to a struct, like below, structs different if classes make a copy of the value when it is passes instead pass a reference.
struct usersAvailable {
var username = String()
var userAvatar = UIImage()
}
这篇关于斯威夫特 - 麻烦来自追加数组类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!