【发布时间】:2016-11-09 14:49:19
【问题描述】:
我有以下几点:
let processURLS = processingViewController()
Timer.scheduledTimer(timeInterval: 1,
target: self,
selector: #selector(processURLS.getURLsToSend),
userInfo: nil,
repeats: true)
触发此事件时,我收到以下错误,我不确定为什么这不起作用
2016-11-09 14:47:00.504932 AcumenJLR[3414:905978] * 终止应用程序 由于未捕获的异常“NSInvalidArgumentException”,原因: '-[AcumenJLR.homeViewController getURLsToSend]:无法识别的选择器 发送到实例 0x100d25c60' * 第一次抛出调用栈:(0x1816721c0 0x1800ac55c 0x181679278 0x181676278 0x18157059c 0x18215c8f8 0x1816208f4 0x181620608 0x18161fec4 0x18161dac0 0x18154c048 0x182fd2198 0x1875372fc 0x187532034 0x100114620 0x1805305b8) libc++abi.dylib:终止于 NSException 类型的未捕获异常
这里是 getURLsToSend 方法
func getURLsToSend () {
//create a fetch request, telling it about the entity
let fetchRequest: NSFetchRequest<URLsToSend> = URLsToSend.fetchRequest()
let context = getContext()
do {
//Get results
let searchResults = try getContext().fetch(fetchRequest)
print ("num of results = \(searchResults.count)")
//You need to convert to NSManagedObject to use 'for' loops
for urls in searchResults as [NSManagedObject] {
//get the Key Value pairs (although there may be a better way to do that...
//print("\(urls.value(forKey: "url"))")
let currentURL = urls.value(forKey: "url")!
//print(urls.value(forKey: "url")!)
completeLoadAction(urlString: currentURL as! String) { code in
if (code == 200){
context.delete(urls)
}
}
}
} catch {
print("Error with request: \(error)")
}
【问题讨论】:
-
发生这种情况是因为控制器类中没有
getURLsToSend函数 -
尝试保留对
processURLS的引用,然后将选择器更改为target: self,.processUrl selector: #selector(getURLsToSend)。我认为您的变量 processURL 已从内存中删除,因此选择器无法执行。更新: -
但似乎选择器也不正确,您必须使用 target =
processUrls和 selector =getURLsToSend