0

I just started a question a few minutes ago were I learned, that class instances in swift are reference types. However as I asked how to pass an instance as copy then or just make a copy inside a function nobody seems to know for sure. So my question is:

Is it possible to pass a class object to a function by value? If yes, how to do so and if no, how can I work with a copy then?

Community
  • 1
  • 1
Nitro.de
  • 821
  • 10
  • 27

3 Answers3

2

It is not possible to pass class objects in Swift by value. What is more, there is no general way of making copies of objects, so you need to provide e.g. appropriate initialiser yourself.

Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51
0

Value objects and reference objects serve different purposes. Asking how to pass a reference object by value is just absolutely pointless. However, a lot of the time you will pass immutable objects, and that means the reference to the object is the value.

By the way: You don't mean "class objects". You mean "instances of a class". In Objective-C, classes are themselves objects. For example, you send the alloc message to a class object.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • I'm coming from c++ were `MyClass foo = MyClass()`are called class objects so yea it's an instance. However if i pass foo to a function it would be a copy (if a copy constructor is available) inside that function if i dont use reference (&). Maybe that make it more clear what i was trying to ask. Anyway thank you for making this clear – Nitro.de Apr 02 '15 at 12:23
0

How to pass reference types by value?

The answer to this question as phrased is you just pass it, by value. It works the exact same way for reference types and value types. Any parameter that is not marked inout in Swift is pass-by-value.

The value of a reference type variable is a reference, which points to an object. When you pass a reference by value, the receiving function receives a copy of this reference, which points to the same object.

Upon further reading of your question, it appears that you are not asking about the passing of reference types at all. Rather, you are asking about the copying of objects. When you wrote "reference type" what you really meant is something like an "object type", something whose value is an object, which when passed by value results in a copy of the object.

Swift has no "object types"; just like Objective-C and Java do not have "object types". It's impossible to have a variable whose value "is an object"; you can only have a variable whose value is a reference that "points to an object". You manipulate objects through these references. There is no syntax in the language to "dereference" a reference to the object it points to.

newacct
  • 119,665
  • 29
  • 163
  • 224
  • Well ty thats the answer i was looking for. Your last sentence makes it clear. However i did not use the "reference type" at all, it just got edited to it. Hower it's good to now that Swift doesn't have "object types", I'm coming out of the c++ world, maybe thats why I just missunderstood the object thing – Nitro.de Apr 07 '15 at 08:17