2

I expect the following code to produce warnings about implicit declaration of functions:

@interface TestClass : NSObject
@end

@implementation TestClass

- (void)foo {
    NSString *test = [self bar];
    NSLog(@"%@", test);
    test = baz();
    NSLog(@"%@", test);
}

- (NSString *)bar {
    return @"bar";
}

NSString *baz() {
    return @"baz";
}

@end

Specifically I would expect warnings about using both bar and baz before they are declared. (bar would be assumed to return id and baz would be assumed to return int.)

GCC shows both warnings, as does LLVM's Clang 2.9. Clang 3, however, can apparently figure out that bar and baz exist and what they return. No warning appears (unless the functions are removed).

(When baz is declared outside of the class, the warning still occurs. So this only applies to Objective-C!)

Awesome! That would allow a lot of duplication to be removed. But what is going on? Is this a language extension? Is it a compiler feature? Is it a bug? Or am I mistaken about this? I couldn't find any documentation on this, so I am wary of relying on it. Does anybody have any insight?

nschum
  • 15,322
  • 5
  • 58
  • 56

2 Answers2

2

The iOS 6 documentation (now out of NDA) finally contains an official acknowledgement of this feature.

It's called "No forward method prototypes needed in @implementation block", requires Xcode 4.3 (LLVM Compiler 3.1) and is compatible with all releases of iOS.

Objective-C Feature Availability Index

That page lists all recent improvements to the compiler and Objective-C and under what constellatons they are available.

nschum
  • 15,322
  • 5
  • 58
  • 56
1

This came up on the Apple list a few weeks ago. It's a new feature. There are some other things coming along like:

NSArray* myArray = @[ @"foo", @"bar", @"baz"];

as syntactic sugar for

NSArray* myArray = [NSArray arrayWithObjects: @"foo", @"bar", @"baz", nil];

(well it's not quite a direct translation).

Also, I think there's going to be

array[i]

for

[array objectAtIndex: i];

and there will be equivalent extensions for dictionaries.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • There's an SO post about the literal syntax: http://stackoverflow.com/questions/9693647/is-there-some-literal-dictionary-or-array-syntax-in-objective-c – jscs Mar 28 '12 at 17:43