Cannot pass immutable value as inout argument: literals are not mutable, why?
Literals cannot be passed as inout parameters since they are intrinsically immutable.
Use two variables instead:
var i=5
var j=8
swapF(a:&i, with: &j)
Furthermore, with one of the last Swift 3 snapshots, inout should be placed near the type, the prototype of your function becomes:
func swapF(a:inout Int, with b:inout Int )
Swift Error: Cannot pass immutable value as inout argument: 'pChData' is a 'let' constant
You are trying to access/modify pChData
argument, which you can't unless or until you declare it as inout
parameter. Learn more about inout
parameter here. So try with the following code.
func receivedData(inout pChData: UInt8, andLength len: CInt) {
var receivedData: Byte = Byte()
var receivedDataLength: CInt = 0
memcpy(&receivedData, &pChData, Int(len));
receivedDataLength = len
AudioHandler.sharedInstance.receiverAudio(&receivedData, WithLen: receivedDataLength)
}
Immutable value as inout argument
For those who has the cannot pass immutable value as inout argument
error. Check that your argument is not optional first. Inout type doesn't seems to like optional values.
Inout not working with SubClass object throws Cannot pass immutable value as inout argument
Since the Plane class is initialized with "init(_ anchor: ARPlaneAnchor)" even it is declared as SCNNode class it returns a different instance than a pure SCNNode as in the Block 2.
I'm not an expert on the mutation subject but think there is an interesting document in Apple blogs The Role of Mutation in Safety
Not really sure but since a class is by nature mutable you probably can move the update function to the Plane class as a (test) solution
Cannot pass immutable value as inout argument: 'CGLayerGetContext' is a function
Please read the code from apple carefully, you have to create the context
private var playerItemContext = 0
and use the property, not the type
guard context == &playerItemContext else {
and CGLayerGetContext
is not related to the code at all
Cannot pass immutable value of type 'NSLayoutConstraint' as inout argument
I solved it by simply declaring the constraint
parameter as an explicitly unwraped NSLayoutConstraint
.
func exchangeConstraint(_ constraint: inout NSLayoutConstraint!) {
...
}
UPDATE
Here is a project where I use it: https://github.com/truffls/compatible-layout-anchors-ios
How to fix 'can not use mutating member on immutable value'?
Well, you also have a problem of removing elements from the array and then going beyond its bounds. Try it like this:
func removeInPlace(item: Int, fromArray: inout [Int]) -> Void {
for i in stride(from: fromArray.count - 1, to: 0, by: -1)
{
if fromArray[i] == item {
fromArray.remove(at: i)
}
}
}
var anarray : [Int] = [1, 2,3, 4 ,3]
var x = 3
print(anarray) // prints [1, 2, 3, 4, 3]
removeInPlace(item: x, fromArray: &anarray)
print(anarray) // prints [1, 2, 4]
You'll want additional error handling, but hope this helps get you on your way.
Inout and NSMutableDictionary
This works not because Cocoa types are exempt, but because NSMutableDictionary
is a class
(as opposed to a struct
), and the inout
does not refer to what you might be thinking.
Unfortunately, the documentation you link to (and the more in-depth documentation on inout
parameters it links to) doesn't make it clear what "value" really means:
An in-out parameter has a value that is passed in to the function, is modified by the function, and is passed back out of the function to replace the original value
The following statement hints at it a little, but could be clearer:
You can only pass a variable as the argument for an in-out parameter. You cannot pass a constant or a literal value as the argument, because constants and literals cannot be modified.
The "value" the documentation describes is the variable being passed as inout
. For value types (struct
s), this is meaningful because every variable holding a value of those types effectively holds a copy of that value.
var a = MyGreatStruct(...)
var b = a
// a and b are not directly linked in any way
Passing a struct
to a function normally copies the value into a new local variable (new variable = copy), whereas you can imagine inout
giving you direct access to the original variable (no new variable).
What's not described is that the effect is identical for classes, which behave differently.
let a = MyGreatClass(...)
let b = a
// modifying `a` will modify `b` too since both point to the same instance
Passing a class
to a function also copies the variable into a new local variable, but the copy isn't meaningful — both variables hold the same thing: a reference to the object itself in memory. Copying in that sense doesn't do anything special, and you can modify the object from inside of the function the same way you could from outside. inout
for classes behaves the same way as for struct
s: it passes the original variable in by reference. This has no bearing on the majority of the operations you'd want to perform on the object anyway (though it does allow you to make the variable point to a different object from within the function):
var a = MyGreatClass("Foo")
// func foo(_ value: MyGreatClass) {
// value = MyGreatClass("Bar") // <- not allowed since `value` isn't mutable
// }
func foo(_ value: inout MyGreatClass) {
value = MyGreatClass("Bar")
}
print(ObjectIdentifier(a)) // <some pointer>
foo(&a)
print(ObjectIdentifier(a)) // <some other pointer>
Related Topics
How to Make One Side of a Div Pointy with CSS
Swift Compiler Error: Use of Unresolved Identifier 'Name'
Do Xcode 8 Swift 3 Apps Run on iOS 7 Successfully
Ionic 3 Response with Status: 0 for Url: Null
Add Udid in Current Provisioning Profile
iOS Lazy Var Uibarbuttonitem Target Issue
iOS Facebook Sdk Error Domain Com.Facebook.Sdk Code 2 and Code 7
Box Shadow Spread Bug in Webkit in iOS 7 on Retina Ipads
Adaptive Launch Screen Storyboards: How to Differentiate iPad Orientations
Cfbundledocumenttype Is Not Working in Myproject-Info.Plist File
How to Check Bitfields (Scnetworkreachabilityflags in Particular) for Flags in Swift
Display: Flex; Doesn't Work on iPad (Both Chrome and Safari)
Get Xcode 5 to Warn About New API Calls
Media_Err_Decode on HTML5 Video in iOS UIwebview After Many Plays