Questions tagged [inout]
98 questions
3
votes
1 answer
Why can't I pass an implicitly unwrapped optional as an UnsafeMutablePointer?
It seems that Xcode 9.3 does fix one issue I was having, but in Swift 4.1 the second half of this code still doesn't compile:
var obj: SomeClass! ; class SomeClass {}
func inoutFunc(_: inout SomeClass?) {}
inoutFunc(&obj) // works
func…

natevw
- 16,807
- 8
- 66
- 90
3
votes
1 answer
Are implicitly unwrapped optionals truly optionals?
In Swift 4.0, the following code doesn't compile:
var str: String!
func someFunc(_ s: inout String?) {}
someFunc(&str)
Now I imagine that str is of type String? in actuality, and the Swift compiler seems to agree:
Cannot pass immutable value of…

natevw
- 16,807
- 8
- 66
- 90
3
votes
0 answers
Accessing a `var` passed as `inout` is undefined behavior?
From the documentation for inout parameters:
In-out parameters are passed as follows:
When the function is called, the value of the argument is copied.
In the body of the function, the copy is modified.
When the function returns, the copy’s value…

lcmylin
- 2,552
- 2
- 19
- 31
2
votes
1 answer
Allow default value to inout parameter
The title might be a little confusing, so let me break this to you.
In Swift, we can have functions with default parameter values, e.g:
func foo(value: Int = 32) { }
And we also can have In-Out parameters, e.g.
func foo(value: inout Int) { }
But,…
2
votes
2 answers
Swift inout how to not copy back property when not changed, to not trigger objects setters
according to the docs
You write an in-out parameter by placing the inout keyword at the
start of its parameter definition. An in-out parameter has a value
that is passed in to the function, is modified by the function, and is
passed back out…

Peter Lapisu
- 19,915
- 16
- 123
- 179
2
votes
1 answer
VHDL INOUT Port does not provide a signal (I2C)
as a part of a project, I am trying to implement a very basic I2C module (not self written) into my project. The I2C module uses the data line as an inout port. I am trying to verify the Data "01010001", containing the 6 bit address + 'read' bit. I…
user10622651
2
votes
2 answers
How to update a value in a nested dictionary given path fragment in Swift?
I have a nested dictionary and an array containing path fragment. I need to update the value at that location.
I am possibly looking for a recursive function instead of extensions to Dictionary types and such.
I am not able to do this recursively…

John Doe
- 2,225
- 6
- 16
- 44
2
votes
2 answers
Assinging inout port to inout in both directions
For simulation purposes, using Verilog, I would like to create a block that only has 2 inout ports (say left and right) and will be transmitting signals either from left to right or from right to left. It can be assumed that the block will be active…

M.Eren Çelik
- 155
- 2
- 9
2
votes
2 answers
"implicit conversion from to requires a temporary" error when passing a tuple as an inout argument
This is my code:
var myTuple = ("bar", 42)
func foo(_ bar: inout (arg1: String, arg2: Double)) {
[...]
}
foo(&myTuple)
I get the following error for this line:
foo(&myTuple)
Cannot pass immutable value as inout argument: implicit conversion…

Edward Alric
- 61
- 2
2
votes
2 answers
Memory safety of “+=” operator in Swift
I’ve been learning swift and encountered a question about memory safety. The += operator takes an inout parameter on the left, which should have write access over the entire function call. And it do something like left = right+left within its…

Antonia Zhang
- 103
- 11
2
votes
2 answers
Assigning value to inout parameters within closure in Swift 3
I have an error when I try to assign a value to the function parameter while inside a completion block, I get an error that reads 'escaping closures can only capture inout parameters explicitly by value' .
How could I fix this? Any tip is much…
user6354073
2
votes
2 answers
Replacing characters in a parameter string
I want to wipe an input string. Let's start with this:
func foo(s: String) {
s.replaceSubrange(0..

Raphael
- 9,779
- 5
- 63
- 94
2
votes
1 answer
Swift cast protocol type to struct and pass as inout
protocol ValueHolder {
}
struct A: ValueHolder {
var value = 5
}
var x: ValueHolder = A()
func f(a: inout A) {
a.value = 10
}
I want to use pass x to f. Is it possible?
Edit: I understand all of the staff about value semantics and inout…

timaktimak
- 1,380
- 1
- 12
- 21
2
votes
3 answers
Avoid using inout in VHDL
I want to avoid using inout at the following code.
Is there any way I can do it? For example a helping signal?
entity LA_Unit is
Port ( Cin : in STD_LOGIC;
P : in STD_LOGIC_VECTOR (3 downto 0);
G : in …

Katerina Tsellou
- 67
- 2
- 11
2
votes
1 answer
Inout parameters don't have the same address
I have three classes A, B and C. A has a resource called rA. What I am trying to achieve is that all of those instances have a reference to the exact same resource.
So to concrete in Swift terms:
Class A has a property called foo:
private var foo…

the_critic
- 12,720
- 19
- 67
- 115