0

Swift has the inout keyword to pass a primitive argument by reference. When would I use this over just passing it by value?

Edit: I realize that you can use this to change its value, but why not just pass it by value and assign it the corresponding value in the tuple returned by the function?

tldr
  • 11,924
  • 15
  • 75
  • 120

2 Answers2

6

You would do that if you wanted to modify the original value instead of just a copy. However, I would argue that you should just return the new value since you can return multiple values in Swift.

drewag
  • 93,393
  • 28
  • 139
  • 128
  • I also would argue that you should just return the new value since you can return multiple values in Swift. – zaph Jun 15 '14 at 22:02
2

This seems to be a plausible reason:

"Maybe the existing body of Objective C libraries have a lot of out parameters, and they didn't want to wrap them all for Swift."

http://blog.lexspoon.org/2014/06/my-analysis-of-swift-language.html

tldr
  • 11,924
  • 15
  • 75
  • 120
  • 1
    I believe that is the reason that the feature exists at all in the language. I'm not sure if that is a valid reason to create new APIs that use inout. – drewag Jun 15 '14 at 22:43