views:

168

answers:

3

The short version is that I have a protocol which has an optional parameter. When I build a class that implements it with the iPhone SDK 3.x it compiles just fine, with no errors or warnings. When I used the 2.x SDK I get the following warning:

Class.m:68: warning: property 'field' requires method '-field' to be defined - use @synthesize, @dynamic or provide a method implementation

It works just fine in both cases.

So two questions:

  1. What is the right way to fix the warning? I added @dynamic to the implementation which isn't really correct as the property really isn't there.
  2. Why does work in SDK 3.x but not 2.x? The docs say "On Mac OS X v10.5, protocols may not include optional declared properties." Clearly that's not exactly the case here.

Here's a quick sample of the kind of code I have to make things a little more obvious if I wasn't completely clear.

@protocol MyProtocol
  @required
    - (void) method:(NSString*)param;
  @optional
    @property (nonatomic,retain) NSString* field;
@end

@interface MyClass : NSObject<MyProtocol> {
}
- (void) method:(NSString*)param;
@end
A: 

The @optional was introduced in Objective-C 2.0 so it won't be applicable for older versions of the SDK. Your best bet is to determine whether it should be present (probably not) and then #ifdef that around with

#if __OBJC2__
@optional
@property ...
#endif

Then it should only compile under an OBJC2, and it won't be present in the older systems as part of the protocol itself.

AlBlue
I believe all versions of iPhone SDK support Objective-C 2.0, which was released with Leopard in late 2007. Recall that the iPhone SDK was not released until mid-2008.
Quinn Taylor
+2  A: 

The iPhone SDK is not exactly identical to any paricular version of Mac OS X. Clearly a newer version of the toolset is included with SDK 3 that's more similar to the one from Snow Leopard.

Chuck
I think you're right. Do you know of a preferred way to eliminate the warning? Or is it just a case of "don't do it" using SDK 2.x?
Stephen Darlington
Well, don't do it or make peace with the warning. It's a feature that's new in SDK 3.
Chuck
A: 

The simnple way to remove the warning is to add

@dynamic field;

to your implementation. That tells the compiler that you will provide the implementation dynamically, which you wont, becuase its an optional property, but that should shut the compiler up.

Peter N Lewis