斯威夫特数组传值...相同的内存地址? [英] Swift Array Pass by Value...same memory address?
问题描述
有人可以请清除此为我说话。
我的理解(思维)斯威夫特
传递数组按值,因为它是一个结构。
但是,当我通过赛格瑞数组传递到下一个视图控制器在我看来,它是通过引用传递,因为当我检查数组它们是相同的内存地址。
这是我如何检查
的println(\\(unsafeAddressOf(运行)))// 0x0000000174240c00
我还以为这些内存地址会有所不同?还是我只是混淆自己。
运行/ StaffTask类无论是从NSObject的继承降耗的目的。
类运行:NSObject的,NSCoding {}
此外,如果我在数组中访问一个项目
VAR的服务= self.staffTasks [indexPath.row]
和编辑该值,无论该服务变量的值和阵列中的元素被更新。他们有相同的内存地址,如图
的println(\\(unsafeAddressOf(服务))\\(unsafeAddressOf(self.staffTasks [indexPath.row])))
另外...
staffTasks是一个较大的阵列称为运行的一个子集
当我搜索服务对象,具有较大的盘,我发现他们也是相同的内存地址
如果让指数=查找(self.runs,self.staff){
的println(本地\\(unsafeAddressOf(self.staff))主目录\\(unsafeAddressOf(self.runs [指数])))
}
我使用NSCoding救我的对象,所以我想我需要找到服务对象大集内,更换对象,然后保存出来。
但原来我不需要,我能够编辑服务变量,运行的阵列自动更新......像一个引用类型会。
传递? ..设定瓦尔
在prepare为SEGUE,只是用标准的方式设置在第二VC的局部变量,VAR runsInSecondVC =运行。不使用,和放大器;指针或任何其他古怪
感谢您的帮助。
类基本详情
类运行:NSObject的,NSCoding { VAR runDate:NSDate的!
VAR staffName:串!
VAR staffEmail:串!
VAR runTasks:[StaffTask]!}类StaffTask:NSObject的,NSCoding { VAR TASKNAME:字符串
VAR taskTime:NSInteger的
VAR CLIENTNAME:串!
}
这是这些类的基础。没有什么复杂的。
按值传递数组
块引用>没有。斯威夫特阵列是一个的值类型的。这并不意味着他们是通过按值。这意味着你可以通过变异的引用方式数组,而不会影响其他引用到该数组已被分配。
Can someone please clear this up for me.
I understand (thought)
Swift
passes arrays by value as it is a struct.But when I pass an array via segue to the next view controller it appears to me that it is passing by reference, as when I check the memory address of the array they are the same.
This is how I'm checking
println("\(unsafeAddressOf(runs))") // 0x0000000174240c00
I would have thought these memory addresses would be different ? Or am I just confusing myself.
The Run / StaffTask classes both inherit from NSObject for saving purposes.
class Run: NSObject, NSCoding { }
Furthermore, if I access a item in the array
var service = self.staffTasks[indexPath.row]
and edit the value, both the service variable value and the element in the array are updated. They have the same memory address, as shown by
println("\(unsafeAddressOf(service)) \(unsafeAddressOf(self.staffTasks[indexPath.row]))")
Also...
staffTasks are a subset of a larger array called runs
When I search for the service object, with the larger set, I find that they are also the same memory address
if let index = find(self.runs, self.staff) { println("local \(unsafeAddressOf(self.staff)) main list \(unsafeAddressOf(self.runs[index]))") }
I am using NSCoding to save my objects, so I thought I would need to find the service object within the larger set, replace that object and then save them out.
But turns out I don't need to, I am able to edit the service variable and array of runs is updated on automatically ... like a reference type would be.
Passing? .. setting the Var
in prepare for segue, just setting the local var in the second VC using the standard way, var runsInSecondVC = runs. Not using, &pointers or any other weirdness.
Thanks for any help.
Class Basic Details
class Run: NSObject, NSCoding { var runDate:NSDate! var staffName:String! var staffEmail:String! var runTasks:[StaffTask]! } class StaffTask: NSObject, NSCoding { var taskName:String var taskTime:NSInteger var clientName:String! }
These are the basic of these classes. Nothing complicated.
解决方案passes arrays by value
No. Swift arrays are a value type. This does not mean they are pass-by-value. It means that you can mutate an array by way of a reference without affecting other references to which that array has been assigned.
这篇关于斯威夫特数组传值...相同的内存地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!