我在AppDelegate中声明了一个新方法。m:
-(void):(UIApplication *)aMethod :(NSDictionary *)launchOptions{
......
[UMessage registerForRemoteNotificationsWithLaunchOptions:launchOptions Entity:entity
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
}else{
}
}];
......
}
在我AppDelegate。h:
- (void)aMethod;
在我anotherClass。m:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate aMethod];
当我在另一个类中运行代码时。我得到了错误。有人知道我错在哪里吗?
###错误的发生是因为。h和。m中的方法签名不匹配,而对于外部类,。h文件是相关的。
但还有一个更严重的错误/误解。实际上你在扩展UIApplicationDelegate,这没有意义。传递一个特定实例作为第一个参数的方法只有在实例中调用时才有用,在UIApplication中你的例子中,delegate被声明了。
在AppDelegate中声明但从任意类调用的方法的签名应该与普通方法相同
。 h
-(void)aMethod:(NSDictionary *)launchOptions;
00
-(void)aMethod:(NSDictionary *)launchOptions {
...
[UMessage registerForRemoteNotificationsWithLaunchOptions:launchOptions
Entity:entity
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
}else{
}
}];
...
}
并使用它
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSDictionary *options = ...
[appDelegate aMethod: options];