我在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]。
而當我在anotherClass.m中運行這段代碼時,我得到了這個錯誤。有誰知道我錯在哪里了嗎?
uj5u.com熱心網友回復:
錯誤的發生是因為.h和.m中方法的簽名不匹配,對于外部類來說,.h檔案是相關的。
但是,還有一個更重要的錯誤/誤解。實際上你正在擴展UIApplicationDelegate,這毫無意義。傳遞特定實體作為第一引數的方法只有在delegate所宣告的實體中被呼叫時才有用,在你的例子中就是UIApplication。
在AppDelegate中宣告但從任意類中呼叫的方法的簽名應該與普通方法相同
.h
-(void)aMethod:(NSDictionary *) launchOptions。
.m
-(void)aMethod:(NSDictionary *) launchOptions {
...
[UMessage registerForRemoteNotificationsWithLaunchOptions: launchOptions
物體:物體
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
}else{
}
}];
...
}
并使用它
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate] 。
NSDictionary *options = ...
[appDelegate aMethod: options]。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/307446.html
標籤:
上一篇:如何在表視圖中添加目錄樹?
