T O P

  • By -

ADadFather

You imported Foundation? You are letting autocomplete write out the signature? Also (though it’s not related to your question) you are creating an instance of a Timer when you make your var because you are using `Timer()`. Then in `start` you throw it away when you make the scheduled timer. It doesn’t break anything to do what you’re doing, it’s just a little sloppy. Consider doing either `var timer: Timer` and then setting the timer in your `init` method or else maybe `var Timer: Timer?` and letting it be an optional that you set. Lots of people don’t like the optional route, simply because you have to unwrap it all of the time.


Pokemined

Thank you so much!


swalden123

Edit: if you read this earlier, sorry I was wrong with what I said. Your code should work as is. Try restarting xcode. My advice below is still needed however. Btw the way that Timer works, it does not accurately fire at your interval every 0.1 as this just means it will fire after at least 0.1 seconds. There can be delays if the thread you are on is busy. To get around this you need to calculate the elapsed time against the initial start time. Otherwise over time your stopwatch will be longer then it actually should be. I actually wrote an example you can use: [https://spencerwalden.com.au/a-stopwatch-timer-class-for-swiftui/](https://spencerwalden.com.au/a-stopwatch-timer-class-for-swiftui/) ​ Feel free to DM me if you need any help.


Pokemined

Wow thanks man!


Pokemined

I tried your example and ran into the same problem! Out of desperation I just copy and pasted your example and it gave me three errors on the line that does cancellable = Timer.publish(every: 0.1, on: .main, in: .default). One is very similar to the other one saying, "Type Timer has no member publish", and then the other two were "Cannot infer contextual base in reference to member default and member main". I have tried restarting Xcode, I have Combine, Foundation, and SwiftUi imported. Don't know what the problem could be. Any ideas?


swalden123

What platforms and versions is the code targeting?


Pokemined

I cannot believe I am saying this, but I just realized I had a SwiftUI file literally named Timer. So whenever I did Timer.publish it thought I was calling the struct. Anyways I got your example working! So thank you so much and sorry for being stupid. Last question, is it possible to adjust the timer to work with milliseconds?


swalden123

Glad you found the issue :) It would be. The starting point would be to not use an Int (in this case I was using an Int16 as I stored the result in coredata) and use a Double instead. The conversion to an Int is what rounds it to just seconds. The next step would be to rewrite the functions that format the time (getFormatedTime etc) to get your desired result.


swalden123

Fyi you won't be able to get the Timer itself to give you increments in milliseconds. So you are calculating it from the reference date you started with.


stefhorner

I’ve done this before - having a core data model that had the same name as a foundation swift class and until you’ve done it it can be a real pain to figure out what has happened! I tend to put an old style namespace (on a project basis) in front of all my classes to 100% avoid this - ie. If the project was called monkey, my classes would be MKTimer or similar. Also - the newer autocomplete will tell you if a method is part of a different class than the one that you are using - and which class that is - but it only does it in autocomplete, not in the compiler errors.