What is the difference between
@interface URLCacheConnection : NSObject {
id <URLCacheConnectionDelegate> delegate;
}
@property (nonatomic, assign) id delegate;
@end
and
@interface URLCacheConnection : NSObject {
}
@property (nonatomic, assign) id<URLCacheConnectionDelegate> delegate;
@end
These two class definitions both seem to behave the same. What is the purpose of defining variables in both the interface and as a property?
First, the two are not quite identical. For the first, the compiler will allow you to assign any object as the delegate, while for the second it will complain if the object you assign doesn’t conform to the URLCacheConnectionDelegate protocol. That’s easily enough fixed, of course.
In earlier versions of Apple’s Objective-C compiler, it was required to explicitly declare the ivar backing a property in order to use @synthesize. At some point (I forget exactly when) they changed it to allow the compiler/runtime to automatically create the needed ivar.