1

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.

5
  • What is the type of the "service" variable? Do your arrays contain reference types (classes) or value types (structs)? Commented Jul 9, 2015 at 0:16
  • 1
    Can you add some more actual code detail of what you're passing around? Commented Jul 9, 2015 at 0:19
  • The arrays are classes, class Task: NSObject, NSCoding { } Commented Jul 9, 2015 at 0:26
  • Please show the types of these runs, service, staffTasks, staff variables Commented Jul 10, 2015 at 0:52
  • I can't see any arrays here that you would be printing. Commented Jul 10, 2015 at 8:52

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

See my book for an explanation of what a value type is: apeth.com/swiftBook/ch04.html#SECreferenceTypes
Everything is pass-by-value unless you have &, but it is irrelevant because there is no passing here.
@newacct Now I'm confused .. ?
@DogCoffee: Maybe there is passing but you have not shown any of it in your question and it is completely unclear what is going on.
Just setting a variable the normal way.
1

You are getting a little confused about the array itself and the contents of the array.

..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...

Putting an object into an array doesn't copy the object. In fact, the array holds a reference to the object, so the line above actually says "make a variable, service that holds a reference to the object that self.staffTasks[indexPath.row] holds a reference to". There is only one object, now referred to in two places, so when you update the object that change is visible through both variables.

2 Comments

Putting anything in an array copies it, in the same way that assignment (=) copies it. Of course if the "thing" is a reference, then the reference is copied. "Objects" are not values directly and can only be accessed through references.
well yes, that is what I said more or less

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.