skip to Main Content

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


  1. func reversed(_ array: inout ([Int]) -> [Int]) {
    

    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] like func 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.

    Login or Signup to reply.
  2. 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 of item.


    func removing(_ item: Int, from array: inout [Int]) -> [Int] {
    

    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.


    var array = array
    

    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 an inout parameter as you never update it.


    var arrayRes : [Int]
    

    Using an initialized variable is very bad practice in Swift, at least you should initialize and empty array.


    for i in array {
        if array[i] != item {
            notFound = false
        }
    }
    

    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:

    for element in array {
      if element != item {
        notFound = false
      }
    }
    

    if notFound == false {
        ...
    } else if array == [] {
        ...
    } else {
        for element in array {
            if element == item { ... }
        }
    }
    

    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 that notFound is necessarily true in the else branch, meaning all elements are different that item, so element == item always returns false.


    array.remove(at: i)
    

    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).


    return arrayRes = removing(item, from: &array[1...array.count])
    

    This is wrong in many ways:

    • you are returning the value of an assignment, i.e a code line with a = sign. This does not make sense as you probably want to return the value of removing(...) rather than the value of the assignment. And an assignment does not have any value (materialized by the Void type).
    • This call (minus the previous error) is recursive, I don’t now if this is intended but I doubt you understand how this works.
    • The removing(...) func accepts an array to [Int] as argument, here you are trying to pass an array Slice array[1...array.count]. Also remove the & sign if not using the inout parameter.
    • I hope that you know that Swift array indices start at 0, not 1 and that the 1 is intended in your code.

    All that said, the origin of your error is here:

    return arrayRes = removing(item, from: &Array(array[1...array.count]))
    

    Array(array[1...array.count]) is an immutable object because is is instantiated inline, at the call site. However, it should be mutable because removing accepts an inout 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 of Void (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:

    var array = [1, 2, 3]
    
    func removing(_ item: Int, from array: inout [Int]) {
      array.removeAll { $0 == item }
    }
    
    removing(2, from: &array)
    print(array)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search