-->

Can't declare another window

2019-08-19 07:32发布

问题:

I'm trying to declare another window in MyThing.m

@property (nonatomic, strong) UIWindow *window;

But get this error

Illegal redeclaration of property in class extension "MyThing" (attribute must be 'readwrite', while its primary must be 'readonly')

If I rename window to something else, it is OK. Why is that? Is window meant to be declared once in the AppDelegate.h ?

回答1:

I figure out the problem, it has nothing to do with the window property declared in AppDelegate.h

The problem is MyThing conforms to UIApplicationDelegate, and UIApplicationDelegate protocol declare a property

@property (nonatomic, retain) UIWindow *window NS_AVAILABLE_IOS(5_0);

So we must do either of these

MyThing.h (like AppDelegate.h does)

@interface MyThing : NSObject <UIApplicationDelegate>

@property (nonatomic, strong) UIWindow *window;

@end

OR

MyThing.m (synthesize the window property declared in protocol)

@implementation WYNAppDelegate

@synthesize window;

@end