In Swift, an Optional is a way to say “this value might be there, or it might be missing.” We use ? to show that a value is optional, like: var name: String? But what is really happening behind the scenes? What an Optional Really Is Inside Swift, an Optional is just a simple enum with two cases: enum Optional<Wrapped> { case none // no value case some(Wrapped) // has a value } That’s all an Optional is! So when you write: var age: Int? Swift actually thinks: var age: Optional<Int> The ? is only a shortcut for us. Why Optionals Feel Special Even though Optionals are simple, Swift gives them extra features: if let and guard let value ?? defaultValue value! for force unwrap a?.b?.c for optional chaining These special features work only with Swift’s built-in Optional. How Swift Stores Optionals Swift stores Optionals in a smart way: Many Optionals take no extra memory compared to the normal value. Swift uses unused bits or adds a small...
Recently I published a countdown app . At one point of development - I have to show a timer on a UILabel which ticks on each seconds. As usual I started setting text to a label object - self .timerLabel.text = someString Easy piece of cake right !? But wait ... it won't take much user attention when timer ticks on every seconds. So I decided to make use of a simple animation while label gets text update. I found there are dozens of ways to animate a label. In this short article, I listed 3 best way you can animate text on a label. ( Spoiler Alert 👀- I decided to go with 3rd option) 1. Fade In - Fade out animation : CATransition class has got transition type `fade`. With timing function of CATransition - I was able to see the below result. let animation: CATransition = CATransition () animation.timingFunction = CAMediaTimingFunction (name: CAMediaTimingFunctionName .easeInEaseOut) animation.type = CATransitionType .fade animation.subtype = C...