J.Miller J.Miller - 7 months ago 276
Swift Question

Swift Error: Cannot pass immutable value as inout argument: 'pChData' is a 'let' constant

I have a function that looks like following.

func receivedData(pChData: UInt8, andLength len: CInt) {
var receivedData: Byte = Byte()
var receivedDataLength: CInt = 0

memcpy(&receivedData, &pChData, Int(len)); // Getting the error here
receivedDataLength = len
AudioHandler.sharedInstance.receiverAudio(&receivedData, WithLen: receivedDataLength)
}


Xcode screenshot

Though non of the argument which i am passing here are not
let
constant. I don't understand why i am getting this. Can anybody please help me and tell why i am getting this ?

Answer

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)
}
Comments