Home > Blockchain >  Function remove(atOffsets:) complains about wrong type only at runtime
Function remove(atOffsets:) complains about wrong type only at runtime

Time:11-10

I have this pretty simple code in Playground. It compiles without errors, but when I try to run it, I get an error with remove(atOffsets:) function. Why is that?

enum CandyColor: CaseIterable {
    case red, green, yellow, brown
}
enum CandyFilling: CaseIterable {
    case chocolade, nuts
}

extension CaseIterable {
    static var random: Self {
        Self.allCases.randomElement()!
    }
}

typealias Candy = (color: CandyColor, filling: CandyFilling)

var candies = (0...20).map{ _ in Candy(color: CandyColor.random, filling: CandyFilling.random)}

var redWithChocolade = [Candy]()
var yellowWithNuts = [Candy]()
var brownAndGreenWithChocolade = [Candy]()

var indiciesToRemove = IndexSet()
for (index, candy) in candies.enumerated() {
    switch candy {
    case (.red, .chocolade):
        redWithChocolade.append(candy)
        indiciesToRemove.insert(index)
    case (.yellow, .nuts):
        yellowWithNuts.append(candy)
        indiciesToRemove.insert(index)
    case let(c) where [.brown, .green].contains(c.color) && c.filling == .chocolade:
        brownAndGreenWithChocolade.append(c)
        indiciesToRemove.insert(index)
    default:
        break
    }
}

candies.remove(atOffsets: indiciesToRemove) // Error: candidate expects value of type 'Int' for parameter #1

print("Left in heap: \(candies)") 

CodePudding user response:

Why don't you use just filter(_:) instead?

let leftOver = candies.filter { candy in 
    switch candy {
    case (.red, .chocolade): 
        redWithChocolade.append(candy)
        return false
    case (.yellow, .nuts):
         yellowWithNuts.append(candy)
         return false
     case (.brown, .chocolade):
         fallthrough
     case (.green, .chocolade):
         brownORGreenWithChocolade.append(candy)
         return false
      default: return true
    }
}

Or in case you really need in-place removal, removeAll(where:):

candies.removeAll { candy in 
    switch candy {
    case (.red, .chocolade): 
        redWithChocolade.append(candy)
        return true
    case (.yellow, .nuts):
         yellowWithNuts.append(candy)
         return true
     case (.brown, .chocolade):
         fallthrough
     case (.green, .chocolade):
         brownORGreenWithChocolade.append(candy)
         return true
      default: return false
    }
}

CodePudding user response:

You should add:

import SwiftUI

to your script because it contains remove(atOffsets:) implementation and XCode doesn't import this framework implicitly.

  • Related