Swift Using Timer

As in most programming languages, time is also very important in Swift. Especially the present time is usually when we are in the practice of growing up. Of course, this program may show changes in the program. To use time in Swift we need to define a variable and get that variable from Timer class. To summarize the Timer class in brief, it is the class where we have the functions to grow in our business. Without writing a lot of code on this page, we can reach what we want.

Let’s make a narrative of them through a scenario. First, let’s take the time and date now. For this we define two variables named TimeFormatter and DateFormatter. After that, we need to give some style to these variables. If the time is a date through timeStyle, then we give it a dateStyle. The purpose for which we give these pictures is concerned with how much information we provide about time or date. For example, if you select Style Medium, you will see the time, minute, and second, while selecting full in the style section will give you the screen as much as I use the time zone. On this count, we can get time and seconds.

        let saatFormatter = DateFormatter()
        saatFormatter.timeStyle=DateFormatter.Style.medium
        
        let tarihFormatter = DateFormatter()
        tarihFormatter.dateStyle=DateFormatter.Style.medium

If you want to display the current time and date continuously on the screen, you have to use the timer. For this, we created a variable called a counter. We will use this to take the time again. We can do this with the function below. To explain the function, timeInerval tells you how many seconds it will work. When you ask where you will give it as your goal, you will target yourself. As a selector you also specify which function will work. Inside this function you will also add the time and date to the top and drop them into a label and refresh it within the time you want and display it in the labs.

sayac = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(self.guncelle)), userInfo: nil, repeats: true)

If you have any questions, please contact me by email or comment.

Leave a Reply

Your email address will not be published. Required fields are marked *

Press ESC to close