Ios 将代码块指定给特性目标c
我正在尝试在iOS应用程序中进行后台应用程序刷新。然而,我在理解代码块时遇到了一些困难 我已经对它做了一些研究,可以说到目前为止我对它的理解还是初学者的。所讨论的方法是:Ios 将代码块指定给特性目标c,ios,cocoa-touch,objective-c-blocks,codeblocks,uiapplication,Ios,Cocoa Touch,Objective C Blocks,Codeblocks,Uiapplication,我正在尝试在iOS应用程序中进行后台应用程序刷新。然而,我在理解代码块时遇到了一些困难 我已经对它做了一些研究,可以说到目前为止我对它的理解还是初学者的。所讨论的方法是: -(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ 此方法需要UIBackgroundFetchResul
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
此方法需要UIBackgroundFetchResult返回类型。但由于我的应用程序的复杂性,我无法轻松地返回它。在后台模式下从互联网上提取数据时会发生很多事情
在该方法的主体中,我有一个自定义方法,它也有一个完成块。我试图做的是在代码中定义另一个自定义方法,该方法将被分配给完成处理程序
在我的数据管理器中,我有一个属性定义为:
@property (copy, nonatomic) void (^fetchCompleted)(UIBackgroundFetchResult);
在performFetchWtihCompletionHandler方法实现中,我调用我的数据管理器:
-(void)fetchNewDataWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
_fetchCompleted = completionHandler;
_state = DMSMT_WaitingForPartnerData;
[self getActiveQueues];
}
下载完成后,我调用fetchCompleted方法:
[self fetchCompleted];
这就是我的问题所在。我需要传递一个UIBackgroundFetchResult参数,但我看不到这样做的方法。我尝试了[self-fetchCompleted:UIBackgroundFetchResultNewData];但它对我大喊大叫
有什么想法吗
提前谢谢
编辑:
这就是解决办法。太简单了
if(_fetchCompleted != nil){
[self fetchCompleted](UIBackgroundFetchResultNewData);
}
您将fetchCompleted视为一个方法,但它是一个块!试试这个:
-(void)getActiveQueues {
// Do some work here
// When you are finished...
// Set the appropriate result
UIBackgroundFetchResult result;
// Check to make sure fetchCompleted is not NULL
if (self.fetchCompleted) {
// Invoke the block!
self.fetchCompleted(result);
}
}
您将fetchCompleted视为一个方法,但它是一个块!试试这个:
-(void)getActiveQueues {
// Do some work here
// When you are finished...
// Set the appropriate result
UIBackgroundFetchResult result;
// Check to make sure fetchCompleted is not NULL
if (self.fetchCompleted) {
// Invoke the block!
self.fetchCompleted(result);
}
}
此方法需要UIBackgroundFetchResult返回类型
否,它需要一个void
返回类型。其中一个参数的类型为UIBackgroundFetchResult
。参数不是返回结果UIBackgroundFetchResult
只是一种变量类型
这似乎导致了你的错误[self fetchCompleted]
是返回fetchCompleted
变量的getter。它和它没有任何关系
要执行块,请使用类似函数的语法。例如,[self-fetchCompleted]()
此方法需要UIBackgroundFetchResult返回类型
否,它需要一个void
返回类型。其中一个参数的类型为UIBackgroundFetchResult
。参数不是返回结果UIBackgroundFetchResult
只是一种变量类型
这似乎导致了你的错误[self fetchCompleted]
是返回fetchCompleted
变量的getter。它和它没有任何关系
要执行块,请使用类似函数的语法。例如,
[self-fetchCompleted]()
谢谢!这很有效。我讨厌这么简单的语法问题!谢谢!这很有效。我讨厌这么简单的语法问题!啊