Monday, March 12, 2012

Objective-C Operators

Gwynne Raskind:

I do strongly support the concept of @== and @!= operators that equate to [object isEqual:] and its negation, as well as the associated @<, @> etc. operators. If one of the objects in question can be determined not to implement compare:, throw a compile error. If either operand is typed id, throw a runtime exception, exactly as [(id)[[NSObject alloc] init] compare:] would do now. In short, make the operators mere syntactic sugar, just like dot-syntax and the collection literals, rather than trying to toy with the runtime as @"" does.

The new collection literals and indexing syntax are no longer under NDA. The full details (e.g. the meaning of negative indexes) are still forthcoming, but it looks as though Apple got this right. Raskind’s proposal seems like a good next step.

3 Comments RSS · Twitter

When declaring objectAtIndexedSubscript:, you can use any integer or enum type as parameter type.

Most clang tests (and even comment in the source code) use size_t, which is an unsigned type.

So, I think the meaning of negative indexes may be different for each class that declare this function using a signed integer.

That said, you may be talking about the NSArray specific case. Maybe the final release of the SDK will contain methods declared using NSUInteger instead of NSInteger to match other existing methods.

@Jean-Daniel I’m hoping that negative indexes will be defined to be relative to the end of the array.

The documentation is now publicly available at http://clang.llvm.org/docs/ObjectiveCLiterals.html

“The meaning of indexes is left up to the declaring class. The compiler will coerce the index to the appropriate argument type of the method it uses for type-checking. For an instance of NSArray, reading an element using an index outside the range [0, array.count) will raise an exception. For an instance of NSMutableArray, assigning to an element using an index within this range will replace that element, but assigning to an element using an index outside this range will raise an exception; no syntax is provided for inserting, appending, or removing elements for mutable arrays.”

Leave a Comment