Category Archives: objective c

Generics in ObjC

“Why would I want generics?”

you may ask. And for good reason: often we use classes that works against arbitrary types. Apple makes it all the time. Just look at the NSArray or NSDictionary classes for example.
But lets see the benefits of using generics:

  • When you use arbitrary type, there is nothing to prevent you to accidentally use a wrong object type. For example adding an NSString to a collection, but expecting an object when you retrieve the data. This may lead to nasty bugs, even crashes, that may be revealed at runtime only.
  • Using generics enables code completion
  • Using generics makes the object’s interface visible without cast. For example if I have a class named Foo that has a property named ‘status’, you could access that property for anĀ  simply by calling: [array objectAtIndex:index].status;

Continue reading Generics in ObjC