0
id<UIView> views = @[one, two, three];  
NSInteger object = [views lastObject]; // Compiler will warn me that `views` stores only UIViews

How far would I have to go implement this? Does anybody have some experience with extending clang to support similar features?

Similar: nsmutablearray-force-the-array-to-hold-specific-object-type-only

Community
  • 1
  • 1
Michael
  • 1,212
  • 1
  • 14
  • 17
  • Wait. You're willing to extend the compiler...yourself? – zneak Apr 20 '13 at 00:46
  • The better solution is to dupe one of the many radars open for this to improve its chances of being added officially to the language (http://openradar.appspot.com/radar?id=2731402 is one example). Modifying ObjC this way is really not something I would recommend one do independently. – Rob Napier Apr 20 '13 at 00:50
  • 1
    You can't. The closest I've seen is [Typed Collections with Self Types in Objective-C](http://www.jonmsterling.com/posts/2012-02-05-typed-collections-with-self-types-in-objective-c.html). – Jano Apr 20 '13 at 01:22
  • PS `NSInteger` is not an object, it's a typedef for some primitive type :) – Jack Lawrence Apr 20 '13 at 02:40

2 Answers2

0

I have thought about this also, though in the end I ask would it actually make me more productive. Objective-C seems to me to be a very pragmatic language, the features it has are real world useful, things like block are super useful, but features like namespaces and typed arrays in my experience not so much. I add a lot of NSAssert to my code to check stuff like that. Usually my mutable collections are contained within other classes and so I have a lot of control over what can be added to them, but maybe thats a pattern I adopt because I don't have typed collections?

Nathan Day
  • 5,981
  • 2
  • 24
  • 40
0

Well, technically you already can...

UIView *views[] = {one, two, three};  
NSInteger object = views[2]; 

Or, more usefully, you could use Objective-C++.

Chris Devereux
  • 5,453
  • 1
  • 26
  • 32