I get the above message in a project, I was trying to test the code and needed the ability to debug.
//
// main.swift
// Chapter7Example2
//
// Created by Mark Edward King on 20/04/2021.
//
import Foundation
func removingOnce(_ item: Int, from array: inout [Int]) -> [Int] {
for i in 0..<array.count {
if array[i] == item {
// Swap the first two elements
array.remove(at: i)
return array
}
}
return array
}
var arrayVal : [Int] = [1, 2, 3, 4, 5]
arrayVal = removingOnce( 2, from: &arrayVal)
for i in arrayVal {
print(i)
}
func removing(_ item: Int, from array: inout [Int]) -> [Int] {
var notFound = true
var arrayRes : [Int] = []
for i in array {
if array[i] != item {
notFound = false
}
}
if notFound == false {
return array
} else if array == [] {
return array
} else {
for i in array {
if array[i] == item {
// Remove the first element
array.remove(at: i)
return arrayRes = removing(item, from: &Array(array[1..<array.count]))
}
}
return array
}
}
func reverse( from array: inout [Int]) -> [Int] {
var arrayRes : [Int] = []
if array == [] {
return array
}
else if array.count == 1 {
return array
}
else {
// Swap first two members
array.swapAt(0, 1)
// Break down the array into smaller parts
arrayRes = reverse( &Array(array[2..<array.count]))
// Append one of the arrays which has been reversed and append the solved part
return array[0...1] += arrayRes
}
}
var arrayVal2 : [Int] = [1, 2, 2, 2, 5]
arrayVal2 = removing( 2, from: &arrayVal2)
for i in arrayVal2 {
print(i)
}
/*func middle(_ array: [Int]) -> Int? {
if array.count % 2 == 0 {
return array[array.count / 2 - 1]
}
else {
return array[array.count / 2]
}
}*/
var arrayVal3 : [Int] = [1, 2, 3, 4, 5]
I just need help with the removing function. There are actually two errors but. The other error is cannot convert return type of () to [Int], not sure why this is related so help on this problem would also be appreciated.
2
Answers
Is a function that takes a single argument: an inout function of type [Int] -> [Int] – not what you want
You should either make it a function that mutates a [Int] via an inout [Int] parameter: like
func reversed(_ array: inout [Int])
or else make it a function from [Int] -> [Int] likefunc reversed(_ array: [Int]) -> [Int]
Functions of the form A -> A are equivalent to functions of the form inout A -> Void, and you should pick one style or the other.
There are errors at almost every lines of your
removing
function, I’ll try to address each one assuming that your function is supposed to remove every occurence ofitem
.As said by @Shadowrun, you must choose between returning the modified array as the return type or updating it via the
inout
parameter, not both.Here you make a local copy of
array
with a shadowing name (i.e. you use the same name), which defeats the purpose of using aninout
parameter as you never update it.Using an initialized variable is very bad practice in Swift, at least you should initialize and empty array.
You tried to iterate over the array elements instead of the array indices. The fast that the compiler did not complained is by pure coincidence as array elements are also of
Int
type, the same as Swift array indices.Another issue is that you don’t really need indices but only elements, here is the correct way:
What is inside the if condition of the else branch will never be executed because you previously checked that
notFound == false
in the first if condition. This means thatnotFound
is necessarilytrue
in the else branch, meaning all elements are different thatitem
, soelement == item
always returnsfalse
.Doing an operation that mutates the array you are currently iterating is almost always guaranteed to be an error and will certainly cause you troubles and runtime errors at some point (do it only if you know that it is safe).
This is wrong in many ways:
=
sign. This does not make sense as you probably want to return the value ofremoving(...)
rather than the value of the assignment. And an assignment does not have any value (materialized by theVoid
type).removing(...)
func accepts an array to[Int]
as argument, here you are trying to pass an arraySlice
array[1...array.count]
. Also remove the&
sign if not using theinout
parameter.All that said, the origin of your error is here:
Array(array[1...array.count])
is an immutable object because is is instantiated inline, at the call site. However, it should be mutable becauseremoving
accepts aninout
parameter, which requires the parameter to be mutable.The second error is at the same line, you are trying to return the value of an assignment (statement with an
=
sign) instead of the return value of the function. An assignment has a return type ofVoid
(meaning that it does not return something useful) which is incompatible with your function return type:[Int]
.I don’t know if this is homework or some kind of programming training, but if this is the case you should start with the basis, the Official Swift Book by Apple is a good starting point. If not the case you can achieve the same thing with: