Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

296
Visualizações
Checking if object is not of type in swift

I need to check if an object is not of a certain type. I know in Kotlin it's possible to check if a type is not by using !is. I was wondering if there was an equivalent in Swift, or a workaround I could use if it's not possible?

Example in Kotlin:

Assume animals is an interface of different animals and there are classes that implement it such as Cat, Dog, Horse, Spider, etc:

var animals = listOf<Animals>(Horse(), Cat(), Dog(), Spider())
var chosenAnimals = animals.filter { it !is Spider }
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Using swift syntax this is one way to do this

let animals: [Animals] = [Horse(), Cat(), Dog(), Spider()]
var chosenAnimals = animals.filter { type(of: $0) != Spider.self }

alternatively

var chosenAnimals = animals.filter { !($0 is Spider) }
over 4 years ago · Santiago Trujillo Relatório

0

Or, you can create your own isNot function, like this:

extension Animal {
    func isNot<T: Animal>(_ type: T.Type) -> Bool {
        return !(self is T)
    }
}

print(Horse().isNot(Fish.self)) // prints true
print(Horse().isNot(Horse.self)) // prints false
over 4 years ago · Santiago Trujillo Relatório

0

you can use this:

var chosenAnimals = animals.filter({!($0 is Spider)})
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda