簡體   English   中英

無法識別的選擇器在函數調用中發送到實例

[英]Unrecognized selector sent to instance in function call

我在計時器中調用一個函數,該函數引發以下錯誤:

AppName.CollectionViewController lastActive]: unrecognized selector sent to instance

計划觸發計時器時,應用程序崩潰並向右拋出異常。 這是計時器及其調用的函數:

       var timer = NSTimer.scheduledTimerWithTimeInterval(180, target: self, selector: Selector("lastActive:"), userInfo: nil, repeats: true)
        }

        func lastActive(){
            if PFUser.currentUser(){
                PFCloud.callFunctionInBackground("registerActivity", withParameters: [:], target: nil, selector: Selector("lastActive:"))
            }
        }

還有其他方法可以在計時器中調用該函數嗎? 我需要在“選擇器:”參數中輸入什么?

謝謝!

編輯:這是整個班級:

import UIKit

let reuseIdentifier = "Cell"

class LobbyCollectionViewController: UICollectionViewController, UICollectionViewDataSource {

    var lobbyData:NSMutableArray = NSMutableArray()


    override func viewDidAppear(animated: Bool) {

        if (!PFUser.currentUser()){
            var loginAlert:UIAlertController = UIAlertController(title: "Sign Up / Login", message: "Enter your login information and press the Log In button to log into the app. If you are new, enter in the desired login information and press the Sign Up button.", preferredStyle:UIAlertControllerStyle.Alert)

            loginAlert.addTextFieldWithConfigurationHandler({
                textfield in
                textfield.placeholder = "Username"
            })

            loginAlert.addTextFieldWithConfigurationHandler({
                textfield in
                textfield.placeholder = "Password"
                textfield.secureTextEntry = true
            })


            loginAlert.addAction(UIAlertAction(title: "Login", style: UIAlertActionStyle.Default, handler: {
                alertAction in
                let textFields:NSArray = loginAlert.textFields as NSArray
                let usernameTextField:UITextField = textFields.objectAtIndex(0) as UITextField
                let passwordTextField:UITextField = textFields.objectAtIndex(1) as UITextField

                PFUser.logInWithUsernameInBackground(usernameTextField.text, password: passwordTextField.text){
                    (user:PFUser!, error:NSError!)->Void in
                    if (user){
                        println("Login successful")
                    } else {
                        println("Login failed")
                    }
                }
                }))


            loginAlert.addAction(UIAlertAction(title: "Sign Up", style: UIAlertActionStyle.Default, handler: {
                alertAction in
                let textFields:NSArray = loginAlert.textFields as NSArray
                let usernameTextField:UITextField = textFields.objectAtIndex(0) as UITextField
                let passwordTextField:UITextField = textFields.objectAtIndex(1) as UITextField

                var user:PFUser = PFUser()
                user.username = usernameTextField.text
                user.password = passwordTextField.text

                user.signUpInBackgroundWithBlock{
                    (success:Bool!, error:NSError!)->Void in
                    if !error{
                        println("Sign Up successful")
                    }else{
                        let errorString = error.userInfo["error"] as NSString
                        println(errorString)
                    }

                }

            }))

            self.presentViewController(loginAlert, animated: true, completion: nil)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        if PFUser.currentUser(){

            self.loadData()

            PFCloud.callFunctionInBackground("registerActivity", withParameters: [:], target: nil, selector: "block:")

            var timer = NSTimer.scheduledTimerWithTimeInterval(180, target: self, selector: Selector("lastActive:"), userInfo: nil, repeats: true)
            //(timeInterval: 180, target: self, selector: "registerActivity", userInfo: nil, repeats: true)
        }

        func lastActive(){
            if PFUser.currentUser(){
                PFCloud.callFunctionInBackground("registerActivity", withParameters: [:], target: nil, selector: "block:")
            }
        }


        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Register cell classes
        self.collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }

    func loadData(){
        lobbyData.removeAllObjects()

        if PFUser.currentUser(){

            PFCloud.callFunctionInBackground("getOnlineUsers", withParameters: [:], target: nil, selector: "block:")

            func block(users: NSArray, error:NSError){
                if(error != nil){

                    self.lobbyData = users as NSMutableArray
                }
            }

            //var timer = NSTimer.scheduledTimerWithTimeInterval(180, target: self, selector: "block:", userInfo: nil, repeats: true)

        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

    // MARK: UICollectionViewDataSource

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView!) -> Int {
        //#warning Incomplete method implementation -- Return the number of sections
        return 1
    }


    override func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int {
        //#warning Incomplete method implementation -- Return the number of items in the section
        return lobbyData.count
    }

    override func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {
        //let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell

        // Configure the cell

        var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as FightCollectionViewCell
        let battle:PFUser = self.lobbyData.objectAtIndex(indexPath!.row) as PFUser



        return cell
    }

還有更詳細的錯誤:

[AppName.LobbyCollectionViewController lastActive:]: unrecognized selector sent to instance 0x7ffc60c34100
AppName[435:6742] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AppName.LobbyCollectionViewController lastActive:]: unrecognized selector sent to instance 0x7ffc60c34100'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001117343e5 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000113247967 objc_exception_throw + 45
    2   CoreFoundation                      0x000000011173b4fd -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x00000001116937ec ___forwarding___ + 988
    4   CoreFoundation                      0x0000000111693388 _CF_forwarding_prep_0 + 120
    5   Foundation                          0x0000000111b67e94 __NSFireTimer + 83
    6   CoreFoundation                      0x000000011169c4d4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    7   CoreFoundation                      0x000000011169c095 __CFRunLoopDoTimer + 1045
    8   CoreFoundation                      0x000000011165f3cd __CFRunLoopRun + 1901
    9   CoreFoundation                      0x000000011165e9f6 CFRunLoopRunSpecific + 470
    10  GraphicsServices                    0x00000001153a69f0 GSEventRunModal + 161
    11  UIKit                               0x0000000111fbd990 UIApplicationMain + 1282
    12  AppName                           0x000000010fff808e top_level_code + 78
    13  AppName                           0x000000010fff80ca main + 42
    14  libdyld.dylib                       0x00000001137cf145 start + 1
    15  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

如果您使用的選擇器末尾帶有“:”,它將查找帶有參數的選擇器。 嘗試使用“ lastActive”或為發送上下文的“自身”添加參數。

由給選擇器的名稱標記的功能必須是目標的成員。 即,在上面的代碼中,您將目標指定為“ self”,這是指LobbyCollectionViewController類的實例。 但是,您的函數func lastActive()已嵌入在override func viewDidLoad()函數內-這意味着它不是LobbyViewController的成員(這是一種非常不典型的編碼方式,可以在函數內包含不是閉包的函數) 。 要解決此問題,請按如下所示重構:

override func viewDidLoad() {
    super.viewDidLoad()

    if PFUser.currentUser(){

        self.loadData()

        PFCloud.callFunctionInBackground("registerActivity", withParameters: [:], target: nil, selector: "block:")

        var timer = NSTimer.scheduledTimerWithTimeInterval(180, target: self, selector: Selector("lastActive"), userInfo: nil, repeats: true)
        //(timeInterval: 180, target: self, selector: "registerActivity", userInfo: nil, repeats: true)
    }

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Register cell classes
    self.collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    // Do any additional setup after loading the view.
}


func lastActive(){
    if PFUser.currentUser(){
        PFCloud.callFunctionInBackground("registerActivity", withParameters: [:], target: nil, selector: "block:")
        }
}

請注意,該函數現在與viewDidLoad處於同一級別,並且選擇器沒有':'。 希望對您有用!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM