为什么我在使用 NSTimer 每隔 x 秒执行一次 运行 函数时会收到此错误消息?

Why am I getting this error message when I am using NSTimer to run function every x seconds?

我正在使用 NSTime 运行 按照建议每 x 秒执行一次函数 here

我的问题是,每当 x 秒数到达并且该执行该功能时,我都会收到此错误消息。

2015-04-06 11:55:51.898 myapp[21611:1186926] -[myapp.ViewController getJson(sharedData.jsonUrl)]: unrecognized selector sent to instance 0x7fff48761d60
2015-04-06 11:55:51.902 myapp[21611:1186926] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[myapp.ViewController getJson(sharedData.jsonUrl)]: unrecognized selector sent to instance 0x7fff48761d60'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000101497a75 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000102fefbb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010149ed1d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x00000001013f69dc ___forwarding___ + 988
    4   CoreFoundation                      0x00000001013f6578 _CF_forwarding_prep_0 + 120
    5   Foundation                          0x00000001018cf844 __NSFireTimer + 83
    6   CoreFoundation                      0x00000001013ff6c4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    7   CoreFoundation                      0x00000001013ff285 __CFRunLoopDoTimer + 1045
    8   CoreFoundation                      0x00000001013c259d __CFRunLoopRun + 1901
    9   CoreFoundation                      0x00000001013c1bc6 CFRunLoopRunSpecific + 470
    10  GraphicsServices                    0x00000001055a0a58 GSEventRunModal + 161
    11  UIKit                               0x0000000101d2a580 UIApplicationMain + 1282
    12  maybe                               0x0000000100ed55ee top_level_code + 78
    13  myapp                               0x0000000100ed562a main + 42
    14  libdyld.dylib                       0x00000001037cb145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

我知道该功能有效,因为我已经对其进行了单独测试。

我用来调用 NSTimer 函数的代码是:

override func viewDidLoad() {
    super.viewDidLoad()
    var timer = NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: "getJson(sharedData.jsonUrl)", userInfo: nil, repeats: true)
}

我不确定为什么会收到此错误。我已经尝试从调用和函数本身中删除参数,以便它与我从中学到的 SO 问题中的代码完全相同,但我没有运气。

我正在 swift 中写这篇文章。

这里有更多信息。 getJson 只是一个下载 JSON 文件并将一些值附加到数组的函数。 sharedData 是一个单例 class - 我用它来存储需要在不同视图控制器中使用的值。 jsonUrl 是 class 中的变量之一。所以 sharedData.jsonUrl 只是从 class 中获取变量。\

编辑:

这是我现在尝试使用的代码: var 计时器 = NSTimer.scheduledTimerWithTimeInterval( 0.2, 目标:自己, 选择器:"getJson:", 用户信息:无, 重复:真)

确切的错误信息是:

2015-04-06 16:16:56.160 myapp[22712:1284267] -[myapp.settingsViewController getJson:]: unrecognized selector sent to instance 0x7f8c88c7d810
2015-04-06 16:16:56.165 myapp[22712:1284267] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[myapp.settingsViewController getJson:]: unrecognized selector sent to instance 0x7f8c88c7d810'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010a491a75 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010bfe9bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010a498d1d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010a3f09dc ___forwarding___ + 988
    4   CoreFoundation                      0x000000010a3f0578 _CF_forwarding_prep_0 + 120
    5   Foundation                          0x000000010a8c9844 __NSFireTimer + 83
    6   CoreFoundation                      0x000000010a3f96c4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    7   CoreFoundation                      0x000000010a3f9285 __CFRunLoopDoTimer + 1045
    8   CoreFoundation                      0x000000010a3bc59d __CFRunLoopRun + 1901
    9   CoreFoundation                      0x000000010a3bbbc6 CFRunLoopRunSpecific + 470
    10  GraphicsServices                    0x000000010e59aa58 GSEventRunModal + 161
    11  UIKit                               0x000000010ad24580 UIApplicationMain + 1282
    12  myapp                               0x0000000109ed41ce top_level_code + 78
    13  myapp                               0x0000000109ed420a main + 42
    14  libdyld.dylib                       0x000000010c7c5145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

获取getJson函数是:

func getJson() {
    let urlPath = sharedData.jsonUrl.absoluteString
    let url: NSURL = NSURL(string: urlPath!)!
    let session = NSURLSession.sharedSession()
    let task = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in

        if error != nil {
            // If there is an error in the web request, print it to the console
            println(error.localizedDescription)
        }

        var err: NSError?


        var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary

        if err != nil {
            // If there is an error parsing JSON, print it to the console
            println("JSON Error \(err!.localizedDescription)")
        }

        let json = JSON(jsonResult)

        var teamCount = json["teamCount"].string! // Number in array
        var teamCountInt: Int = teamCount.toInt()! // Converts to integer

        for index in 0...teamCountInt - 1 { // loop for number in array
            let teamJson = json["teams"][index]["\(index)"].string! //
            sharedData.teamsArray.append(teamJson) // Appends to array in shared data
        }
        println(sharedData.teamsArray) // For debug - delete after
    })
    task.resume()
}

此函数使用Swifty-json.

你的post是乱码。计时器调用中包含格式错误的降价 link,因此很难判断我们在看什么。

不过,查看错误消息表明您的选择器是 getJsonTeams(sharedData.jsonUrl),这是无效的。您将选择放在 Objective-C 语法中,没有括号也没有参数。您的选择器可能应该是 getJsonTeams:

编辑:

您的代码应为

var timer = NSTimer.scheduledTimerWithTimeInterval(
  0.2, 
  target: self, 
  selector: "getJson:", 
  userInfo: nil, 
  repeats: true)

选择器应采用 Objective-C 语法,并且绝不应包含括号或参数。如果该方法采用单个参数,则选择器名称应以冒号结尾。

修改getJson()方法如下:

class SomeGoodClassName {
    func getJson(timer: NSTimer) {
        //Directly use shareData.jsonUrl here to get json data

        //... more code
    }
}

NSTimer初始化

let jsonLoader = SomeGoodClassName()

override func viewDidLoad() {
    super.viewDidLoad()
    var timer = NSTimer.scheduledTimerWithTimeInterval(0.2, target: jsonLoader, selector: "getJson", userInfo: nil, repeats: true)
}