Coder Social home page Coder Social logo

objective-c-generics's Introduction

Objective C Generics

Objective C Generics is an effort to try to support basic generics in Objective C.

#Usage#

Copy ObjectiveCGenerics.h to your project. When defining a new class use the GENERICSABLE macro.

#import "ObjectiveCGenerics.h"

GENERICSABLE(MyClass)

@interface MyClass : NSObject<MyClass>

@property (nonatomic, strong) NSString* name;

@end

Now you can use generics with arrays and sets just as you normally do in Java, C#, etc. code

objective-c-generics's People

Contributors

tomersh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

objective-c-generics's Issues

bug with arrays and literals

Hi
awesome project :)

I found a minor bug when there is more than one "generics-abled" object and one uses literals to access objects in a collection instead of objectAtIndex: syntax.
I set up a sample project to demonstrate the bug.

You'll find a warning in AppDelegate.m:28 that shouldn't just be there.

Ciao
ac

Same pattern for custom classes

Hey,

I'm just curious whether the same pattern is applicable to custom collection classes. I tried to but somehow the compiler doesn't use the category defining the typed methods.

So, does that pattern only work with Foundation classes?

Thanks, Oli

Code with two GENERICSABLE classes doesn't compile

Here is an example with two GENERICSABLE classes: Foo and Qux.

#import "Foo.h"
#import "Qux.h"
#import <Foundation/Foundation.h>

int main(void)
{
    @autoreleasepool {
        NSArray<Foo> *fooArray = [NSArray array];
        NSString *bar = [fooArray lastObject].bar;

        NSArray<Qux> *quxArray = [NSArray array];
        NSArray<Foo> *array = [NSArray arrayWithArray:quxArray];
    }
}

Well, it doesn't compile

$ clang -Wall -Wextra -c -fobjc-arc main.m
main.m:8:23: warning: incompatible pointer types initializing 'NSArray<Foo> *__strong' with an expression of type 'NSArray<Qux> *' [-Wincompatible-pointer-types]
        NSArray<Foo> *fooArray = [NSArray array];
                      ^          ~~~~~~~~~~~~~~~
main.m:9:47: error: property 'bar' not found on object of type 'Qux *'
        NSString *bar = [fooArray lastObject].bar;
                                              ^
main.m:12:23: warning: incompatible pointer types initializing 'NSArray<Foo> *__strong' with an expression of type 'NSArray<Qux> *' [-Wincompatible-pointer-types]
        NSArray<Foo> *array = [NSArray arrayWithArray:quxArray];
                      ^       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 warnings and 1 error generated.

It seems that the last imported GENERICSABLE class wins.

Collection properties of genericsable classes also forced to use protocol

Hi, thanks for the code, this could be really helpful!

I'm having a problem using Genericsable on classes which contain collection properties.

Say your MyClass example had another property, an NSArray called myArray. When I compile that, Xcode insists that any objects inside [MyClass myArray] also needs to be of type MyClass. Does this work for you?

Also, in your example you have an NSArray<NSString> -- how do you genericise NSString or any built-in class?

Cheers,
Nick

Doesn't verify whether the objects within the container is kind of the declared class.

I really like this idea. Generics is one of the language features I'm missing in Objective-C when coming from Java.

I did a small test, and it seems though, that the macro doesn't check whether the objects in the container has the correct type as declared. For example, following code is not marked as error and can be executed:

NSArray<NSString> *testStringArray = @[ @1, @"test2" ];
NSLog(@"%@", testStringArray);

It would be great if type checking could be implemented at the objects level.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.