Calling a static method from NSTimer. Is it possible?

Luke Bartolomeo

Is it allowed to call a static method from an NSTimer? The compiler will not allow this, complaining with the cryptic "Extra argument 'selector' in call.

   struct MyStruct {
        static func startTimer() {
            NSTimer.scheduledTimerWithTimeInterval(1.0, target: MyStruct.self, selector: "doStuff", userInfo: nil, repeats: true)
        }

        static func doStuff() {
            println("Doin' it.")
        }
    }

   MyStruct.startTimer()

But of course, this works fine...

class MyClass {
    func startTimer() {
        NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "doStuff", userInfo: nil, repeats: true)
    }

    func doStuff() {
        println("Doin' it.")
    }
}

var instanceOfClass = MyClass()
instanceOfClass.startTimer()

Do I just have the syntax wrong, or is it not allowed?

Vatsal Manot

NSTimer utilizes the Objective-C runtime in order to dynamically invoke methods. When declare a struct, you are using the Swift runtime, therefore it is not possible for NSTimer to cooperate. Structures differ from classes, and you can read more about them here.

Also, a static function is the equivalent of a class method in Objective-C, so if that was your original objective then the following should suffice:

class MyClass: NSObject {
    class func startTimer() {
        NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "doStuff", userInfo: nil, repeats: true)
    }

    class func doStuff() {
        println("Doin' it.")
    }
}

MyClass.startTimer()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Javascript

Calling an overridden static method from parent

From Java

calling static method in java

From Java

Calling static method in python

From Java

calling a super method from a static method

From Java

Calling static method on a class?

From Java

Calling the instance of a static method

From Java

Is it possible to get the class name from a static method?

From Java

Calling get (array static method) from an array instance

From Java

Calling Java Main method from static block

From Dev

calling static method from inside the class

From Dev

Calling a static void Java method from JNI

From Dev

Calling a static template method from a template function

From Dev

Calling a static method from a non-static context

From Dev

Calling a non-static method from outside the class

From Dev

Calling ES6 class constructor from class static method

From Dev

Is it possible to call a static method from withing another static method

From Dev

Calling a static method from a generic constraint Dart

From Dev

Calling a Java static method with generics from Scala

From Dev

Calling super static method from designated initializer

From Dev

Calling a static jar class method from command line

From Dev

Calling static method from m-file (octave/matlab)

From Dev

Is calling a static method from an instance method thread safe?

From Dev

PHP: is it possible to call a static class method from another static class?

From Dev

Calling a static method from cshtml file

From Dev

Calling non static method from static method USING instance created in the static method

From Dev

Calling a static Swift Method from within Obj-C

From Dev

Calling a non static method from a in a static context

From Dev

Calling a non static method using blazor interop from js

From Dev

Calling static method from inside a task

Related Related

  1. 1

    Calling an overridden static method from parent

  2. 2

    calling static method in java

  3. 3

    Calling static method in python

  4. 4

    calling a super method from a static method

  5. 5

    Calling static method on a class?

  6. 6

    Calling the instance of a static method

  7. 7

    Is it possible to get the class name from a static method?

  8. 8

    Calling get (array static method) from an array instance

  9. 9

    Calling Java Main method from static block

  10. 10

    calling static method from inside the class

  11. 11

    Calling a static void Java method from JNI

  12. 12

    Calling a static template method from a template function

  13. 13

    Calling a static method from a non-static context

  14. 14

    Calling a non-static method from outside the class

  15. 15

    Calling ES6 class constructor from class static method

  16. 16

    Is it possible to call a static method from withing another static method

  17. 17

    Calling a static method from a generic constraint Dart

  18. 18

    Calling a Java static method with generics from Scala

  19. 19

    Calling super static method from designated initializer

  20. 20

    Calling a static jar class method from command line

  21. 21

    Calling static method from m-file (octave/matlab)

  22. 22

    Is calling a static method from an instance method thread safe?

  23. 23

    PHP: is it possible to call a static class method from another static class?

  24. 24

    Calling a static method from cshtml file

  25. 25

    Calling non static method from static method USING instance created in the static method

  26. 26

    Calling a static Swift Method from within Obj-C

  27. 27

    Calling a non static method from a in a static context

  28. 28

    Calling a non static method using blazor interop from js

  29. 29

    Calling static method from inside a task

HotTag

Archive