Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

295
Views
Comprobando si el objeto no es de tipo en Swift

Necesito verificar si un objeto no es de cierto tipo. Sé que en Kotlin es posible comprobar si un tipo no lo es usando !is . Me preguntaba si había un equivalente en Swift, o una solución alternativa que podría usar si no es posible.

Ejemplo en Kotlin:

Supongamos que animals es una interfaz de diferentes animales y hay clases que la implementan, como 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 answers
Answer question

0

Usando sintaxis rápida, esta es una forma de hacer esto

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

alternativamente

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

0

O bien, puede crear su propia función isNot, como esta:

 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 Report

0

puedes usar esto:

 var chosenAnimals = animals.filter({!($0 is Spider)})
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!