On Oct 14, 2010, at 1:25 PM, Tito Ciuro wrote:
> I'm seeing a weird behavior in Xcode 3.2.4/GCC 4,2. The warning I'm getting
> is: missing sentinel in function call
>
> This is how I have defined the methods:
>
> + (NSFSomeClass*)someClassWithObjects:(NSArray *)someObjects
> {
> return [[[self alloc]initWithObjects:someObjects]autorelease];
> <<<<<<<<<<<< warning occurs here
> }
>
> - (id)initWithObjects:(NSArray *)someObjects
> {
> if (self = [self init]) {
> ...
> }
>
> return self;
> }
>
> However, if I change the class method to the following, the warning
> disappears:
>
> + (NSFSomeClass*)someClassWithObjects:(NSArray *)someObjects
> {
> return [[[self alloc]initWithObjects:someObjects, nil]autorelease];
> }
>
> Any idea why this is happening? Why does it require a sentinel when I'm
> passing an array?
There's already a method -[NSArray initWithObjects:], but it accepts a
nil-terminated list of objects. The compiler warns if you call [array
initWithObjects:a, b, c] and forget the nil terminator.
`[self alloc]` returns `id`, so the compiler has to guess which method
prototype to use, yours or NSArray's. If it guesses wrong, you'll get that
warning. Sometimes you'll get crashes or incorrect parameter values when the
compiler guesses wrong, but in this case the generated code happens to work
correctly.
I'd recommend changing your method name. -initWithArray: would work, matching
-[NSArray initWithArray:(NSArray*)array].
The other workaround is to cast the result of +alloc: [(NSFSomeClass*)[self
alloc] initWithObjects:someObjects]. But that's fragile if you forget somewhere.
> Another observation:
>
> GCC 4.0 = no warning
> GCC 4.2 = warning
> LLVM GCC 4.2 = warning
> LLVM compiler 1.5 = no warning
>
> Is this a GCC 4.2/LLVM GCC4.2 bug?
No. gcc-4.0 simply does not implement the sentinel warning for Objective-C
methods. If gcc-4.0 guesses wrong, there's no warning and the code happens to
run correctly.
> And while we're at it... since the library I'm writing works on both Mac OS X
> and iOS, which compiler is recommended?
I'd recommend llvm-gcc on all current platforms and architectures.
--
Greg Parker [email protected] Runtime Wrangler
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]