Questions tagged [inout]
90 questions
63
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,273
- 2
- 11
- 24
12
votes
4 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,195
- 8
- 24
- 48
7
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
- 444
- 5
- 16
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,207
- 7
- 35
- 68
6
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
- 181
- 1
- 1
- 7
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
- 601
- 5
- 21
5
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
- 415
- 5
- 14
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
- 675
- 1
- 7
- 14
5
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
- 56,823
- 7
- 97
- 157
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
- 129
- 4
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
- 113
- 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
- 11,837
- 31
- 115
- 194
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
- 13,661
- 7
- 58
- 81
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
- 13,661
- 7
- 58
- 81
3
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
- 799
- 4
- 18