I have the following syntax in Swift
:
func basicFunction(anArray:[Int], aValue:Int) -> Int {
for (var i = 0; i<5; ++i)
{
if anArray[i] == 0
{
anArray[i] = aValue //I get an error in XCode
}
}
return 1
}
I get the following Xcode error
: '@lvalue $T5
' is not identical to 'Int
'
What am I doing wrong?
Function arguments are immutable by default, and the Swift compiler gives terrible error messages.
Anyway, because anArray
is immutable, you cannot modify it. That's why you get an error message. Declare it inout
:
func basicFunction(inout anArray:[Int], aValue:Int) -> Int {
for (var i = 0; i<5; ++i) {
if anArray[i] == 0 {
anArray[i] = aValue
}
}
return 1
}
Call it with an &
in front of the array argument:
basicFunction(&someArray, 99)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With