whats the equivalent of java's instanceof in Swift?












16















Just like java's instanceOf keyword whats the equivalent in Swift?



java example:



A a = new A();
boolean isInstanceOfA = a instanceof A;


Here isInstanceOfA is true



So i need something similar in Swift










share|improve this question


















  • 2





    try operator "is": a is A

    – nerowolfe
    Jun 1 '16 at 8:30













  • nevermind I found the answer in one of stackoverflow chat rooms

    – Bhargav
    Jun 1 '16 at 8:31











  • or maybe I should delete this question if its already answer, a link to a similarly asked question would be good.

    – Bhargav
    Jun 1 '16 at 8:32






  • 1





    You can use is operator An example can be found here: stackoverflow.com/a/36900103/4791032

    – Bogdan Ustyak
    Jun 1 '16 at 8:32
















16















Just like java's instanceOf keyword whats the equivalent in Swift?



java example:



A a = new A();
boolean isInstanceOfA = a instanceof A;


Here isInstanceOfA is true



So i need something similar in Swift










share|improve this question


















  • 2





    try operator "is": a is A

    – nerowolfe
    Jun 1 '16 at 8:30













  • nevermind I found the answer in one of stackoverflow chat rooms

    – Bhargav
    Jun 1 '16 at 8:31











  • or maybe I should delete this question if its already answer, a link to a similarly asked question would be good.

    – Bhargav
    Jun 1 '16 at 8:32






  • 1





    You can use is operator An example can be found here: stackoverflow.com/a/36900103/4791032

    – Bogdan Ustyak
    Jun 1 '16 at 8:32














16












16








16


1






Just like java's instanceOf keyword whats the equivalent in Swift?



java example:



A a = new A();
boolean isInstanceOfA = a instanceof A;


Here isInstanceOfA is true



So i need something similar in Swift










share|improve this question














Just like java's instanceOf keyword whats the equivalent in Swift?



java example:



A a = new A();
boolean isInstanceOfA = a instanceof A;


Here isInstanceOfA is true



So i need something similar in Swift







ios swift






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jun 1 '16 at 8:28









BhargavBhargav

5,41912551




5,41912551








  • 2





    try operator "is": a is A

    – nerowolfe
    Jun 1 '16 at 8:30













  • nevermind I found the answer in one of stackoverflow chat rooms

    – Bhargav
    Jun 1 '16 at 8:31











  • or maybe I should delete this question if its already answer, a link to a similarly asked question would be good.

    – Bhargav
    Jun 1 '16 at 8:32






  • 1





    You can use is operator An example can be found here: stackoverflow.com/a/36900103/4791032

    – Bogdan Ustyak
    Jun 1 '16 at 8:32














  • 2





    try operator "is": a is A

    – nerowolfe
    Jun 1 '16 at 8:30













  • nevermind I found the answer in one of stackoverflow chat rooms

    – Bhargav
    Jun 1 '16 at 8:31











  • or maybe I should delete this question if its already answer, a link to a similarly asked question would be good.

    – Bhargav
    Jun 1 '16 at 8:32






  • 1





    You can use is operator An example can be found here: stackoverflow.com/a/36900103/4791032

    – Bogdan Ustyak
    Jun 1 '16 at 8:32








2




2





try operator "is": a is A

– nerowolfe
Jun 1 '16 at 8:30







try operator "is": a is A

– nerowolfe
Jun 1 '16 at 8:30















nevermind I found the answer in one of stackoverflow chat rooms

– Bhargav
Jun 1 '16 at 8:31





nevermind I found the answer in one of stackoverflow chat rooms

– Bhargav
Jun 1 '16 at 8:31













or maybe I should delete this question if its already answer, a link to a similarly asked question would be good.

– Bhargav
Jun 1 '16 at 8:32





or maybe I should delete this question if its already answer, a link to a similarly asked question would be good.

– Bhargav
Jun 1 '16 at 8:32




1




1





You can use is operator An example can be found here: stackoverflow.com/a/36900103/4791032

– Bogdan Ustyak
Jun 1 '16 at 8:32





You can use is operator An example can be found here: stackoverflow.com/a/36900103/4791032

– Bogdan Ustyak
Jun 1 '16 at 8:32












5 Answers
5






active

oldest

votes


















18














isKindOfClass() method, from NSObjectProtocol is the equivalent of java's instanceof keyword, in java it's a keyword but in swift it's a protocol method, but they behave similarly and are used in similar contexts.




isKindOfClass: returns YES if the receiver is an instance of the
specified class or an instance of any class that inherits from the
specified class.




Which is exactly what instanceof keyword does in Java related link



Example:



let a: A = A()
let isInstanceOfA: Bool = a.isKindOfClass(A) // returns true.


Also you can use the is keyword



let a: A = A()
let isInstanceOfA: Bool = a is A


The difference:




  • is works with any class in Swift, whereas isKindOfClass() works only with those classes that are subclasses of NSObject or otherwise implement NSObjectProtocol.


  • is takes a type that must be hard-coded at compile-time. isKindOfClass: takes an expression whose value can be computed at runtime.



So no is keyword doesn't work like instanceof






share|improve this answer





















  • 1





    AFAIK there aren't any cases where isKindOfClass is required. I would strongly suggest the answer is amended to make clear that is would be the preferred usage.

    – PeejWeej
    Sep 1 '17 at 21:58











  • @PEEJWEEJ the intention here is to provide all available options and what exactly it does thereby giving a "Complete" answer.

    – Bhargav
    Sep 4 '17 at 4:13






  • 1





    Yeah and that's fine, but given that it's Objective-C and not Swift and thus will be unavailable on any platform not including the Objective-C runtime, isKindOfClass is a platform specific alternative, not the Swift equivalent.

    – PeejWeej
    Sep 4 '17 at 17:39



















14














let a = A()
let isInstanceOfA = a is A





share|improve this answer
























  • is keyword works like instanceof?

    – Bhargav
    Jun 1 '16 at 8:45











  • yes, same as instanceof

    – ZHZ
    Jun 1 '16 at 8:57











  • ill give it a try

    – Bhargav
    Jun 1 '16 at 9:06











  • then this begs the question whats the difference between isKindOfClass method and is keyword

    – Bhargav
    Jun 1 '16 at 9:25








  • 1





    is takes a type that must be hard-coded at compile-time. isKindOfClass: takes an expression whose value can be computed at runtime.

    – Bhargav
    Jun 1 '16 at 9:53





















10














For swift3 and swift4 it's:



if someInstance is SomeClass {
...
}


if your class is extending NSObject you can also use:



if someInstance.isKind(of: SomeClass.self) {
...
}





share|improve this answer


























  • This is actually valid from Swift 1.0

    – Sulthan
    Nov 22 '18 at 15:34



















0














For Swift 4.x it's:



func getOrElse<T>(defaultVal:T) -> T {
if let selfVal = self, selfVal is T {
return selfVal as! T
} else {
return defaultVal
}
}


Very short variant(by suggestion @Sulthan):



extension Optional {
func getOrElse<T>(defaultVal:T) -> T {
return (self as? T) ?? defaultVal
}
}





share|improve this answer





















  • 1





    You know that this would be easier to implement using (self as? T) ?? defaultVal?

    – Sulthan
    Nov 22 '18 at 15:35











  • Unlike Java, in Swift optionals are a native type and there are language operators that work with them. Thats why you rarely have to define new methods on Optional - the necessary funcionality is already there.

    – Sulthan
    Nov 23 '18 at 6:53



















-1














With objective-c it's isKindOfClass:[ClassName class].



With swift it's isKindOfClass(Classname.class()).






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37563319%2fwhats-the-equivalent-of-javas-instanceof-in-swift%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    5 Answers
    5






    active

    oldest

    votes








    5 Answers
    5






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    18














    isKindOfClass() method, from NSObjectProtocol is the equivalent of java's instanceof keyword, in java it's a keyword but in swift it's a protocol method, but they behave similarly and are used in similar contexts.




    isKindOfClass: returns YES if the receiver is an instance of the
    specified class or an instance of any class that inherits from the
    specified class.




    Which is exactly what instanceof keyword does in Java related link



    Example:



    let a: A = A()
    let isInstanceOfA: Bool = a.isKindOfClass(A) // returns true.


    Also you can use the is keyword



    let a: A = A()
    let isInstanceOfA: Bool = a is A


    The difference:




    • is works with any class in Swift, whereas isKindOfClass() works only with those classes that are subclasses of NSObject or otherwise implement NSObjectProtocol.


    • is takes a type that must be hard-coded at compile-time. isKindOfClass: takes an expression whose value can be computed at runtime.



    So no is keyword doesn't work like instanceof






    share|improve this answer





















    • 1





      AFAIK there aren't any cases where isKindOfClass is required. I would strongly suggest the answer is amended to make clear that is would be the preferred usage.

      – PeejWeej
      Sep 1 '17 at 21:58











    • @PEEJWEEJ the intention here is to provide all available options and what exactly it does thereby giving a "Complete" answer.

      – Bhargav
      Sep 4 '17 at 4:13






    • 1





      Yeah and that's fine, but given that it's Objective-C and not Swift and thus will be unavailable on any platform not including the Objective-C runtime, isKindOfClass is a platform specific alternative, not the Swift equivalent.

      – PeejWeej
      Sep 4 '17 at 17:39
















    18














    isKindOfClass() method, from NSObjectProtocol is the equivalent of java's instanceof keyword, in java it's a keyword but in swift it's a protocol method, but they behave similarly and are used in similar contexts.




    isKindOfClass: returns YES if the receiver is an instance of the
    specified class or an instance of any class that inherits from the
    specified class.




    Which is exactly what instanceof keyword does in Java related link



    Example:



    let a: A = A()
    let isInstanceOfA: Bool = a.isKindOfClass(A) // returns true.


    Also you can use the is keyword



    let a: A = A()
    let isInstanceOfA: Bool = a is A


    The difference:




    • is works with any class in Swift, whereas isKindOfClass() works only with those classes that are subclasses of NSObject or otherwise implement NSObjectProtocol.


    • is takes a type that must be hard-coded at compile-time. isKindOfClass: takes an expression whose value can be computed at runtime.



    So no is keyword doesn't work like instanceof






    share|improve this answer





















    • 1





      AFAIK there aren't any cases where isKindOfClass is required. I would strongly suggest the answer is amended to make clear that is would be the preferred usage.

      – PeejWeej
      Sep 1 '17 at 21:58











    • @PEEJWEEJ the intention here is to provide all available options and what exactly it does thereby giving a "Complete" answer.

      – Bhargav
      Sep 4 '17 at 4:13






    • 1





      Yeah and that's fine, but given that it's Objective-C and not Swift and thus will be unavailable on any platform not including the Objective-C runtime, isKindOfClass is a platform specific alternative, not the Swift equivalent.

      – PeejWeej
      Sep 4 '17 at 17:39














    18












    18








    18







    isKindOfClass() method, from NSObjectProtocol is the equivalent of java's instanceof keyword, in java it's a keyword but in swift it's a protocol method, but they behave similarly and are used in similar contexts.




    isKindOfClass: returns YES if the receiver is an instance of the
    specified class or an instance of any class that inherits from the
    specified class.




    Which is exactly what instanceof keyword does in Java related link



    Example:



    let a: A = A()
    let isInstanceOfA: Bool = a.isKindOfClass(A) // returns true.


    Also you can use the is keyword



    let a: A = A()
    let isInstanceOfA: Bool = a is A


    The difference:




    • is works with any class in Swift, whereas isKindOfClass() works only with those classes that are subclasses of NSObject or otherwise implement NSObjectProtocol.


    • is takes a type that must be hard-coded at compile-time. isKindOfClass: takes an expression whose value can be computed at runtime.



    So no is keyword doesn't work like instanceof






    share|improve this answer















    isKindOfClass() method, from NSObjectProtocol is the equivalent of java's instanceof keyword, in java it's a keyword but in swift it's a protocol method, but they behave similarly and are used in similar contexts.




    isKindOfClass: returns YES if the receiver is an instance of the
    specified class or an instance of any class that inherits from the
    specified class.




    Which is exactly what instanceof keyword does in Java related link



    Example:



    let a: A = A()
    let isInstanceOfA: Bool = a.isKindOfClass(A) // returns true.


    Also you can use the is keyword



    let a: A = A()
    let isInstanceOfA: Bool = a is A


    The difference:




    • is works with any class in Swift, whereas isKindOfClass() works only with those classes that are subclasses of NSObject or otherwise implement NSObjectProtocol.


    • is takes a type that must be hard-coded at compile-time. isKindOfClass: takes an expression whose value can be computed at runtime.



    So no is keyword doesn't work like instanceof







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jun 1 '16 at 9:53

























    answered Jun 1 '16 at 8:30









    BhargavBhargav

    5,41912551




    5,41912551








    • 1





      AFAIK there aren't any cases where isKindOfClass is required. I would strongly suggest the answer is amended to make clear that is would be the preferred usage.

      – PeejWeej
      Sep 1 '17 at 21:58











    • @PEEJWEEJ the intention here is to provide all available options and what exactly it does thereby giving a "Complete" answer.

      – Bhargav
      Sep 4 '17 at 4:13






    • 1





      Yeah and that's fine, but given that it's Objective-C and not Swift and thus will be unavailable on any platform not including the Objective-C runtime, isKindOfClass is a platform specific alternative, not the Swift equivalent.

      – PeejWeej
      Sep 4 '17 at 17:39














    • 1





      AFAIK there aren't any cases where isKindOfClass is required. I would strongly suggest the answer is amended to make clear that is would be the preferred usage.

      – PeejWeej
      Sep 1 '17 at 21:58











    • @PEEJWEEJ the intention here is to provide all available options and what exactly it does thereby giving a "Complete" answer.

      – Bhargav
      Sep 4 '17 at 4:13






    • 1





      Yeah and that's fine, but given that it's Objective-C and not Swift and thus will be unavailable on any platform not including the Objective-C runtime, isKindOfClass is a platform specific alternative, not the Swift equivalent.

      – PeejWeej
      Sep 4 '17 at 17:39








    1




    1





    AFAIK there aren't any cases where isKindOfClass is required. I would strongly suggest the answer is amended to make clear that is would be the preferred usage.

    – PeejWeej
    Sep 1 '17 at 21:58





    AFAIK there aren't any cases where isKindOfClass is required. I would strongly suggest the answer is amended to make clear that is would be the preferred usage.

    – PeejWeej
    Sep 1 '17 at 21:58













    @PEEJWEEJ the intention here is to provide all available options and what exactly it does thereby giving a "Complete" answer.

    – Bhargav
    Sep 4 '17 at 4:13





    @PEEJWEEJ the intention here is to provide all available options and what exactly it does thereby giving a "Complete" answer.

    – Bhargav
    Sep 4 '17 at 4:13




    1




    1





    Yeah and that's fine, but given that it's Objective-C and not Swift and thus will be unavailable on any platform not including the Objective-C runtime, isKindOfClass is a platform specific alternative, not the Swift equivalent.

    – PeejWeej
    Sep 4 '17 at 17:39





    Yeah and that's fine, but given that it's Objective-C and not Swift and thus will be unavailable on any platform not including the Objective-C runtime, isKindOfClass is a platform specific alternative, not the Swift equivalent.

    – PeejWeej
    Sep 4 '17 at 17:39













    14














    let a = A()
    let isInstanceOfA = a is A





    share|improve this answer
























    • is keyword works like instanceof?

      – Bhargav
      Jun 1 '16 at 8:45











    • yes, same as instanceof

      – ZHZ
      Jun 1 '16 at 8:57











    • ill give it a try

      – Bhargav
      Jun 1 '16 at 9:06











    • then this begs the question whats the difference between isKindOfClass method and is keyword

      – Bhargav
      Jun 1 '16 at 9:25








    • 1





      is takes a type that must be hard-coded at compile-time. isKindOfClass: takes an expression whose value can be computed at runtime.

      – Bhargav
      Jun 1 '16 at 9:53


















    14














    let a = A()
    let isInstanceOfA = a is A





    share|improve this answer
























    • is keyword works like instanceof?

      – Bhargav
      Jun 1 '16 at 8:45











    • yes, same as instanceof

      – ZHZ
      Jun 1 '16 at 8:57











    • ill give it a try

      – Bhargav
      Jun 1 '16 at 9:06











    • then this begs the question whats the difference between isKindOfClass method and is keyword

      – Bhargav
      Jun 1 '16 at 9:25








    • 1





      is takes a type that must be hard-coded at compile-time. isKindOfClass: takes an expression whose value can be computed at runtime.

      – Bhargav
      Jun 1 '16 at 9:53
















    14












    14








    14







    let a = A()
    let isInstanceOfA = a is A





    share|improve this answer













    let a = A()
    let isInstanceOfA = a is A






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jun 1 '16 at 8:38









    ZHZZHZ

    1,570715




    1,570715













    • is keyword works like instanceof?

      – Bhargav
      Jun 1 '16 at 8:45











    • yes, same as instanceof

      – ZHZ
      Jun 1 '16 at 8:57











    • ill give it a try

      – Bhargav
      Jun 1 '16 at 9:06











    • then this begs the question whats the difference between isKindOfClass method and is keyword

      – Bhargav
      Jun 1 '16 at 9:25








    • 1





      is takes a type that must be hard-coded at compile-time. isKindOfClass: takes an expression whose value can be computed at runtime.

      – Bhargav
      Jun 1 '16 at 9:53





















    • is keyword works like instanceof?

      – Bhargav
      Jun 1 '16 at 8:45











    • yes, same as instanceof

      – ZHZ
      Jun 1 '16 at 8:57











    • ill give it a try

      – Bhargav
      Jun 1 '16 at 9:06











    • then this begs the question whats the difference between isKindOfClass method and is keyword

      – Bhargav
      Jun 1 '16 at 9:25








    • 1





      is takes a type that must be hard-coded at compile-time. isKindOfClass: takes an expression whose value can be computed at runtime.

      – Bhargav
      Jun 1 '16 at 9:53



















    is keyword works like instanceof?

    – Bhargav
    Jun 1 '16 at 8:45





    is keyword works like instanceof?

    – Bhargav
    Jun 1 '16 at 8:45













    yes, same as instanceof

    – ZHZ
    Jun 1 '16 at 8:57





    yes, same as instanceof

    – ZHZ
    Jun 1 '16 at 8:57













    ill give it a try

    – Bhargav
    Jun 1 '16 at 9:06





    ill give it a try

    – Bhargav
    Jun 1 '16 at 9:06













    then this begs the question whats the difference between isKindOfClass method and is keyword

    – Bhargav
    Jun 1 '16 at 9:25







    then this begs the question whats the difference between isKindOfClass method and is keyword

    – Bhargav
    Jun 1 '16 at 9:25






    1




    1





    is takes a type that must be hard-coded at compile-time. isKindOfClass: takes an expression whose value can be computed at runtime.

    – Bhargav
    Jun 1 '16 at 9:53







    is takes a type that must be hard-coded at compile-time. isKindOfClass: takes an expression whose value can be computed at runtime.

    – Bhargav
    Jun 1 '16 at 9:53













    10














    For swift3 and swift4 it's:



    if someInstance is SomeClass {
    ...
    }


    if your class is extending NSObject you can also use:



    if someInstance.isKind(of: SomeClass.self) {
    ...
    }





    share|improve this answer


























    • This is actually valid from Swift 1.0

      – Sulthan
      Nov 22 '18 at 15:34
















    10














    For swift3 and swift4 it's:



    if someInstance is SomeClass {
    ...
    }


    if your class is extending NSObject you can also use:



    if someInstance.isKind(of: SomeClass.self) {
    ...
    }





    share|improve this answer


























    • This is actually valid from Swift 1.0

      – Sulthan
      Nov 22 '18 at 15:34














    10












    10








    10







    For swift3 and swift4 it's:



    if someInstance is SomeClass {
    ...
    }


    if your class is extending NSObject you can also use:



    if someInstance.isKind(of: SomeClass.self) {
    ...
    }





    share|improve this answer















    For swift3 and swift4 it's:



    if someInstance is SomeClass {
    ...
    }


    if your class is extending NSObject you can also use:



    if someInstance.isKind(of: SomeClass.self) {
    ...
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 22 '18 at 15:26









    Mariusz Wiazowski

    508513




    508513










    answered Aug 7 '17 at 15:35









    algridalgrid

    2,28121014




    2,28121014













    • This is actually valid from Swift 1.0

      – Sulthan
      Nov 22 '18 at 15:34



















    • This is actually valid from Swift 1.0

      – Sulthan
      Nov 22 '18 at 15:34

















    This is actually valid from Swift 1.0

    – Sulthan
    Nov 22 '18 at 15:34





    This is actually valid from Swift 1.0

    – Sulthan
    Nov 22 '18 at 15:34











    0














    For Swift 4.x it's:



    func getOrElse<T>(defaultVal:T) -> T {
    if let selfVal = self, selfVal is T {
    return selfVal as! T
    } else {
    return defaultVal
    }
    }


    Very short variant(by suggestion @Sulthan):



    extension Optional {
    func getOrElse<T>(defaultVal:T) -> T {
    return (self as? T) ?? defaultVal
    }
    }





    share|improve this answer





















    • 1





      You know that this would be easier to implement using (self as? T) ?? defaultVal?

      – Sulthan
      Nov 22 '18 at 15:35











    • Unlike Java, in Swift optionals are a native type and there are language operators that work with them. Thats why you rarely have to define new methods on Optional - the necessary funcionality is already there.

      – Sulthan
      Nov 23 '18 at 6:53
















    0














    For Swift 4.x it's:



    func getOrElse<T>(defaultVal:T) -> T {
    if let selfVal = self, selfVal is T {
    return selfVal as! T
    } else {
    return defaultVal
    }
    }


    Very short variant(by suggestion @Sulthan):



    extension Optional {
    func getOrElse<T>(defaultVal:T) -> T {
    return (self as? T) ?? defaultVal
    }
    }





    share|improve this answer





















    • 1





      You know that this would be easier to implement using (self as? T) ?? defaultVal?

      – Sulthan
      Nov 22 '18 at 15:35











    • Unlike Java, in Swift optionals are a native type and there are language operators that work with them. Thats why you rarely have to define new methods on Optional - the necessary funcionality is already there.

      – Sulthan
      Nov 23 '18 at 6:53














    0












    0








    0







    For Swift 4.x it's:



    func getOrElse<T>(defaultVal:T) -> T {
    if let selfVal = self, selfVal is T {
    return selfVal as! T
    } else {
    return defaultVal
    }
    }


    Very short variant(by suggestion @Sulthan):



    extension Optional {
    func getOrElse<T>(defaultVal:T) -> T {
    return (self as? T) ?? defaultVal
    }
    }





    share|improve this answer















    For Swift 4.x it's:



    func getOrElse<T>(defaultVal:T) -> T {
    if let selfVal = self, selfVal is T {
    return selfVal as! T
    } else {
    return defaultVal
    }
    }


    Very short variant(by suggestion @Sulthan):



    extension Optional {
    func getOrElse<T>(defaultVal:T) -> T {
    return (self as? T) ?? defaultVal
    }
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 23 '18 at 6:47

























    answered Nov 20 '18 at 6:36









    mr.boyfoxmr.boyfox

    9,18054361




    9,18054361








    • 1





      You know that this would be easier to implement using (self as? T) ?? defaultVal?

      – Sulthan
      Nov 22 '18 at 15:35











    • Unlike Java, in Swift optionals are a native type and there are language operators that work with them. Thats why you rarely have to define new methods on Optional - the necessary funcionality is already there.

      – Sulthan
      Nov 23 '18 at 6:53














    • 1





      You know that this would be easier to implement using (self as? T) ?? defaultVal?

      – Sulthan
      Nov 22 '18 at 15:35











    • Unlike Java, in Swift optionals are a native type and there are language operators that work with them. Thats why you rarely have to define new methods on Optional - the necessary funcionality is already there.

      – Sulthan
      Nov 23 '18 at 6:53








    1




    1





    You know that this would be easier to implement using (self as? T) ?? defaultVal?

    – Sulthan
    Nov 22 '18 at 15:35





    You know that this would be easier to implement using (self as? T) ?? defaultVal?

    – Sulthan
    Nov 22 '18 at 15:35













    Unlike Java, in Swift optionals are a native type and there are language operators that work with them. Thats why you rarely have to define new methods on Optional - the necessary funcionality is already there.

    – Sulthan
    Nov 23 '18 at 6:53





    Unlike Java, in Swift optionals are a native type and there are language operators that work with them. Thats why you rarely have to define new methods on Optional - the necessary funcionality is already there.

    – Sulthan
    Nov 23 '18 at 6:53











    -1














    With objective-c it's isKindOfClass:[ClassName class].



    With swift it's isKindOfClass(Classname.class()).






    share|improve this answer






























      -1














      With objective-c it's isKindOfClass:[ClassName class].



      With swift it's isKindOfClass(Classname.class()).






      share|improve this answer




























        -1












        -1








        -1







        With objective-c it's isKindOfClass:[ClassName class].



        With swift it's isKindOfClass(Classname.class()).






        share|improve this answer















        With objective-c it's isKindOfClass:[ClassName class].



        With swift it's isKindOfClass(Classname.class()).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jun 1 '16 at 9:08









        Aleksander Azizi

        7,60665484




        7,60665484










        answered Jun 1 '16 at 8:39









        PushkrajPushkraj

        1,6751525




        1,6751525






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37563319%2fwhats-the-equivalent-of-javas-instanceof-in-swift%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            鏡平學校

            ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

            Why https connections are so slow when debugging (stepping over) in Java?