Rss Feed
    Showing posts with label Generics. Show all posts
    Showing posts with label Generics. Show all posts
  1. Alternative to instanceType in Swift

    Tuesday, August 23, 2016

    In case you have not noticed, there is no straight forward alternative to using 'instanceType' in Swift. You might have tried using "Self" in place of instanceType like shown in the below code snippet.

    But turns out it gives an error saying the object cannot be converted to type Self. I found this elegant solution posted by Martin R on StackOverflow and it uses Generics.



  2. Generic Stack Implementation In Swift

    Thursday, July 10, 2014

    I'm pretty excited about this new feature in Swift; Generics. This is something borrowed from C++. The `Swift Programming Language` book stated "Generic Code enables you to write flexible, reusable functions and types that can work with any type, subject to requirements you define". Basically it just means you can write functions that can work with multiple types of parameters, collection types that can perform operations on the data set without any concerns of the type of the data set.

    Below is a Gist of Generic Stack Implementation in Swift, a Stack that can work with any data types.


    In the above snippet, you can see, <T> is the additions, right next to class name, that makes it a generic type. You can make use Type T within the implementation just like any other type.

    You can check the sample project here in Github.