Questions tagged [inout]
98 questions
75
votes
5 answers
What does an ampersand (&) mean in the Swift language?
I know about the ampersand as a bit operation but sometimes I see it in front of variable names. What does putting an & in front of variables do?

applejuiceteaching
- 1,473
- 3
- 13
- 25
16
votes
5 answers
Immutable value as inout argument
I would like to have a pointer as a parameter of a class. But when I am trying to code the init, I am having this error: Cannot pass immutable value of type 'AnyObject?' as inout argument
class MyClass {
var valuePointer:…

Tulleb
- 8,919
- 8
- 27
- 55
8
votes
2 answers
Is that an in or in/out parameter? Doxygen, C++
If a pointer is passed to a function for read only, then this pointer is an IN parameter.
If a pointer is passed to a function for read only, but this function makes a copy of the pointer to have access to it in module related functions for read…

aganm
- 1,245
- 1
- 11
- 30
8
votes
1 answer
Swift Generics vs Any
I read swift documentation in apple site.
There is a function swapTwoValues, which swaps two any given values
func swapTwoValues1(_ a: inout T, _ b: inout T) {
let temporaryA = a
a = b
b = temporaryA
}
Now I want to write similar…

Karen Karapetyan
- 704
- 1
- 9
- 18
7
votes
2 answers
How to write to inout port and read from inout port of the same module?
This is not about actually creating a verilog module with inout ports. There are tons of posts I've found about that.
What I am stuck on is, if I have a blackbox module with an inout port, let's says it's defined like
module blackbox(inout a, in b,…

FatherOfNations
- 191
- 1
- 1
- 7
7
votes
1 answer
Using inout keyword: is the parameter passed-by-reference or by copy-in copy-out (/call by value result)
Question: Based on the information and discussion below: Are inout parameters passed-by-reference or by copy-in copy-out?
Based on the following SO threads, function parameters marked by the inout keyword is passed by reference:
Does inout/var…

dfrib
- 70,367
- 12
- 127
- 192
6
votes
1 answer
Cannot pass immutable value of type 'NSLayoutConstraint' as inout argument
I try to reassign an referecend NSLayoutConstraint.
class ViewController: UIViewController {
@IBOutlet weak var myConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
…

Lukas Würzburger
- 6,543
- 7
- 41
- 75
6
votes
2 answers
Is swift inout parameter a variable or a pointer?
I feel a bit lost using swift inout parameter in the following code:
var shouldContinue: Bool = true
func doSomeWork1(shouldContinue: inout Bool)
{
while shouldContinue
{
// ERROR: the compiler wants: doSomeWork2(shouldContinue:…

Adeline
- 465
- 5
- 16
5
votes
4 answers
Is there any difference between "mutating" function and "inout" parameters in Swift?
As per Swift documentation both mutating and inout keywords are used to modify the value types from within a function. Is there any difference between "mutating" and "inout" and any special case where we need to use either of them.

subin272
- 733
- 6
- 24
5
votes
3 answers
Add default value to inout parameter using Swfit
In Swift 2 it is possible to do the following:
class SomeType {
static let singletonInstance = SomeType()
func someFunction(var mutableParameter: SomeType = SomeType.singletonInstance) {
...
}
}
However in Swift 3 the var…

JLau_cy
- 705
- 1
- 7
- 14
4
votes
0 answers
Why can I mutate a let constant with inout parameter when using try?
This will fail to compile with the error:
Immutable value 'self.constantValue' must not be passed inout
class Test {
let constantValue: String = ""
init() {
Test.makeABC(&constantValue)
}
static func makeABC(_…

wshamp
- 131
- 4
4
votes
3 answers
Inout in swift and reference type
I'm trying to understand the difference between value and reference type. And now I want to use function from the apple guide:
func swapTwoInts(_ a: inout Int, _ b: inout Int) {
let temporaryA = a
a = b
b = temporaryA
}
and if I want to use…

Wike
- 53
- 5
4
votes
1 answer
Swift closure capture array by reference
In Swift Collections are pass by value by default and we can user inout to make it pass by reference in function arguments but how can we do it in closure capture variables?
var list = [1, 2, 3]
func edit(inout list: [Int]) {
list.append(4)
…

Alan
- 123
- 1
- 8
4
votes
2 answers
Updating an item in an array passed by reference
What's the easiest/right way to update an item in an array? I want the caller to have the updated array as well. So:
static func updateItem(updatedItem: Item, inout items: [Item]) -> Bool {
var item = items.filter{ $0.id == updatedItem.id…

Prabhu
- 12,995
- 33
- 127
- 210
3
votes
1 answer
Swift 5 : Escaping closure captures 'inout' parameter
I already have the response data that I received from the server. This response data have some bakers data.
Now I want to calculate the distance of the user and bakery and then store it in the same modal class. I have created a function for it. And…

mutAnT
- 464
- 3
- 17