Does Objective-C support Generics?

前端 未结 6 603
后悔当初
后悔当初 2020-12-02 00:07

I wonder whether Objective-C offers any support for generics?

For instance, consider a method:

-(void) sort: (NSMutableArray *) deck {
}
6条回答
  •  时光取名叫无心
    2020-12-02 00:38

    There is an easy, effective way of doing this (I've been using it on projects for a couple of years now). Sadly, someone deleted the answer, and my attempts to get it re-instated were rejected. Here goes again:

    You can re-implement a cut-down version of C++ templating within Obj-C because Obj-C encapsulates all of C (and C++ templates are C-macros with some improved compiler/debugger support):

    This only needs to be done once, using a single header file. Someone has done it for you:

    https://github.com/tomersh/Objective-C-Generics

    You end up with 100% legal Obj-C code that looks like this:

    NSArray anArray= ...
    CustomClass a = anArray[0]; // works perfectly, and Xcode autocomplete works too!
    

    This all works fine in XCode, with autocomplete, etc.

提交回复
热议问题