views:

210

answers:

5

I have a class with only class methods (utility stuff), so my interface is like:

@interface MyUtils : NSObject {
}

Xcode doesn't like it and says:

warning: struct has no named members

So I have to create a nonsense instance variable? My code works fine, though... I just activated every kind of warning for the compiler.

+3  A: 

No, you don't have to create a nonsense instance variable as it's not an error but a warning. The compiler simply found a situation it found to be odd and decided to warn you about it (and only because of your custom warning settings).

In this case, I'd say ignore it (and / or tone down the amount of warnings generated to something more sane).

MathieuK
+1  A: 

Assuming you aren't doing anything weird, this sounds like a compiler bug. A class shouldn't be giving struct warnings.

Chuck
+2  A: 

You've got "Pedantic Warnings" checked, which sets -pedantic for the compiler. The GCC docs refer only to issuing warnings demanded by strict ISO C and ISO C++ for this flag, so I presume Objective-C is doing something behind the scenes that's tripping that warning. Uncheck the setting.

Paul A. Hoadley
+2  A: 

If the lack of instance variables really is the cause behind this warning, did you realise you can just remove the curly braces? e.g.

@interface Foo : NSObject
+ (void)fooBar;
@end

Have a look through the Cocoa headers, many classes are like this.

Mike Abdullah
+3  A: 

The warning itself is not a bug, misleading, or incorrect in any way. You do have a struct, it is empty, and in C, it is unusual to have a struct with no members. The fact that you are seeing the warning is, well... that depends on who you talk to.

Here's your problem: Objective-C is compiled using GCC, which is a C compiler with Objective-C extensions.

Objective-C is a subset of C. Similarly, Objective-C classes are a subset of the C struct.

In Objective C, it is perfectly normal to have no properties in a class. In C, it is quite unusual to have no members in a struct.

And GCC is a C compiler.

This is where your warning is originating from. The Objective-C support in GCC is just not smart enough to get rid of that warning, or you are of the school of thought that it should not.

In the absence of properties in a class, it is customary to remove the curly braces. That won't get rid of the warning, though.

If you ever accidentally create a standard struct (even in Objective-C code), then you will appreciate this warning, because more often than not, an accidentally created struct has no members, and it causes a lot of weird problems.

There are a handful of warnings like this if you enable -pedantic for compiling, and they all have solid roots. Some, like this, are due to subtle differences between Objective-C and C conventions. Others are GCC warning you that your code might not compile on other operating systems. Some people think you should never see them, and others think those messages are perfectly normal.

If you are using Xcode, I would suggest using two targets in your project. One with the normal warnings on, and one with your crazy warnings on. Use the first target for normal coding. Use the second target if you can't identify a bug within 5 minutes. There are numerous tutorials floating around dealing with targets in Xcode.