【问题标题】:Swift 3 timed eventSwift 3 定时事件
【发布时间】: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 = processUrlsselector = getURLsToSend

标签: ios swift timer swift3


【解决方案1】:

根据目标/动作模式,selector 中指定的方法必须在target 中指定的类中声明,在本例中为当前类self

要么更改target,要么实现self中的方法。

【讨论】:

    【解决方案2】:

    可能你在方法中有一些参数,但你没有从那里发送任何值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-06
      • 1970-01-01
      • 2017-05-01
      • 1970-01-01
      • 2017-05-05
      • 1970-01-01
      • 2017-04-20
      相关资源
      最近更新 更多