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

时间:2015-04-06 10:58:18

标签: swift exception runtime-error nstimer

我正在使用NSTime按建议的here每隔x秒运行一个函数。

我的问题是,当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)
}

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

我正在迅速写这篇文章。

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

编辑:

这是我现在尝试使用的代码:     var timer = NSTimer.scheduledTimerWithTimeInterval(                         0.2,                         目标:自我,                         选择器:“getJson:”,                         userInfo:nil,                         重复:真实)

确切的错误消息是:

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

get 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

2 个答案:

答案 0 :(得分:1)

你的帖子是乱码。计时器通话中有一个格式错误的降价链接,因此很难说出我们正在查看的内容。

但是,查看错误消息会显示您的选择器为getJsonTeams(sharedData.jsonUrl),这是无效的。您将选择放在Objective-C语法中,没有括号和参数。你的选择器应该是getJsonTeams:

编辑:

您的代码应该是

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

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

答案 1 :(得分:1)

更改方法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)
}