How to add tolerance to Timers in Swift?

Tolerance indicates the amount of time after the scheduled fire date, the timer may get fired. The timer may get fired at any time between the scheduled fire date and scheduled fire date + tolerance. Providing a tolerance value will enhance the system responsiveness and increased power savings. Adding tolerance to Swift timers neither cause timer to fire early nor it will allow timer to drift.The system may also add additional delay to timers even if tolerance property is set.

 

Example code snippet using Swift playground.

 

 

 

 

 

How to use userInfo parameter in Swift Timers?

userInfo parameter in Timers can be used to send additional information about the context in which the timer is used. The information sent is in the form of dictionary as key-value pair and it can be extracted in the selector method that gets executed once the timer gets fired.

Example code snippet from Playground :

Non-repeating timer in Swift

Creating a non-repeating timer is simple with the following code:

  • timerInterval denotes the number of seconds between the firings of timer.
  • target denotes the object to which send a message specified by selector.
  • selector denotes the message to sent to the target after the timer gets fired.
  • UserInfo can be used to send additional information about timer event in a dictionary format.
  • repeats denotes whether a timer is repeating or non-repeating. If it is set to true, the timer reschedules itself after getting fired automatically until it is manually invalidated and if it is set to false, the timer fires once and gets invalidated automatically.

Repeating Timer in Swift

Creating a repeating timer is simple with the following code.

  • timerInterval denotes the number of seconds between the firings of timer.
  • target denotes the object to which send a message specified by selector.
  • selector denotes the message to sent to the target after the timer gets fired.
  • UserInfo can be used to send additional information about timer event in a dictionary format.
  • repeats denotes whether a timer is repeating or non-repeating. If it is set to true, the timer reschedules itself after getting fired automatically until it is manually invalidated and if it is set to false, the timer fires once and gets invalidated automatically.

 

Timer Class in Swift

Swift provides a Timer class which provides a timer that fires after certain time has elapsed sending a specified message to the target object. Timer is not a real time mechanism. They work in conjunction with runloops. The actual time at which the timer fires can be  significantly later. Timers can be repeating and non-repeating.

 

 

 

 

 

 

How to programatically create a activity indicator in iOS using Swift?

The following code creates a activity indicator programatically.

 

What do you mean by a literal value ?

literal value is a value that appears directly in the source code. When we specify a literal value for a constant or variable, Swift compiler can automatically detect the type of constant or variable.

 

How to automatically return focus to the cell at last focused indexpath in collectionView?

remembersLastFocusedIndexPath

The above boolean property when set to true allows as to return focus to the last collection view cell which was focused before the focus left the collection view. When focus returns to the collection view, the last focused collection view cell will automatically gain focus. By default, this property is set to false.

 

How to automatically return focus to the cell at last focused indexpath in tableView?

remembersLastFocusedIndexPath 

The above boolean property when set to true allows the focus to return to the tableview cell which was focused before the focus left the tableview. When the focus returns to the tableview, the last focused indexpath of the tableview cell will automatically gain focus. By default, this property is set to false.

 

Swift: How ARC works?

Whenever we create a new class instance, ARC allocates a chunk of memory to store information about that Class instance. The information stored includes the type of the instance and stored properties associated with that instance. When the instance is no longer needed, ARC frees up the memory so that it can be used for other purposes. ARC keeps track of all the variables, constants and properties which refer to that particular class instance. ARC will not deallocate an instance as long as any one active reference to that instance exists.