16

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 }
2
  • What is the usecase of this?
    – J. Doe
    Feb 16, 2019 at 21:16
  • When opening a ViewController from a notification, I only need it to do a certain function if the visibleViewController it's not of type MessageViewController, potentially: if (navigationController?.visibleViewController !is MessageViewController) { navigationController?.pushViewController(userProfileVC, animated: false) navigationController?.pushViewController(messageVC, animated: false) } Feb 17, 2019 at 0:11

5 Answers 5

19

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) }
6
  • 1
    @nielsbot, yes but with the negation. Thanks Feb 16, 2019 at 21:53
  • Right. See my answer :)
    – nielsbot
    Feb 16, 2019 at 21:53
  • So basically wrapping the check with a negation. Okay, easy enough. Thanks! Feb 17, 2019 at 0:15
  • 1
    or use mutating method removeAll { $0 is Spider }
    – Leo Dabus
    Feb 17, 2019 at 5:21
  • For me, only the alternative worked. Perhaps because of type casting.
    – PhoenixB
    Aug 25, 2020 at 22:41
4

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
2

There is no equivalent in Swift, but its counterpart is the is operator. You only need to negate the operator result.

var animals = listOf<Animals>(Horse(), Cat(), Dog(), Spider())
var chosenAnimals = animals.filter { !($0 is Spider) }

Or, as you're using a mutating collection (var), you could use removeAll(where:) to remove all Spider instances to solve your problem.

var animals = listOf<Animals>(Horse(), Cat(), Dog(), Spider())
animals.removeAll { $0 is Spider } // animals.removeAll(where: { $0 is Spider })
print("Animals: \(animals)") // should not contain Spider instances
1
  • you can always use Swift trailing closure syntax as well animals.removeAll { $0 is Spider }
    – Leo Dabus
    Feb 17, 2019 at 5:23
1

you can use this:

var chosenAnimals = animals.filter({!($0 is Spider)})
1
  • @JoakimDanielson sorry, I copied @user2759839's question above and almost didn't notice use of val instead of var. thanks for spotting! Feb 16, 2019 at 21:28
1

Here's a working sample in Swift:

protocol Animal { }

class Horse : Animal { }
class Cat : Animal { }
class Dog : Animal { }
class Spider : Animal { }

let animals:[Animal] = [Horse(), Cat(), Dog(), Spider()]
let chosen = animals.filter { !($0 is Spider) }

Fill in your protocols/classes however you like :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.