Posts

Swift Dictionary

What is Swift Dictionary? Swift Dictionary is a hash table providing fast access to the keys it contains.   Entries in the table are identified with the help of key, which is hash able and the entries accessed are objects. In Swift Dictionary is structure with Key and Value, where Key is Hashable as can be understood from the Apple Document  @frozen   struct   Dictionary < Key ,   Value >   where   Key   :   Hashable Let's perform some CRUD operations on a dictionary. Create a dictionary. There are multiple ways to create a dictionary.  Style 1: var userIdNamesDictionary: Dictionary < String , String > = [:] Style 2: var userIdNamesDictionary2:[ String : String ] = [:] Style 3:  Inferring based on the values in the dictionary during initialization. The entry before the colon is key and after the colon is value corresponding to the key, we can add multiple key and value pairs by separating it with comma. var userIdNamesDictionary3 = [ "1" : "Adam",

Why implementation of the sequence protocol is said destructive?

Is implementation of sequence protocol , destructive ? Why and how! Sequence type is said to provide sequential, iterated access of its elements.  Think of a sequence generator which can be used to create user id's, from a starting point.   public struct UserIdSequenceGenerator : Sequence {     private var startId : Int          public init (startId: Int ){         self . startId = startId     } } extension UserIdSequenceGenerator : IteratorProtocol {     public mutating func next () -> Int ? {         defer {             startId += 1         }         return startId        } } Requirements on implementing Sequence Protocol. First Approach. Override  make Iterator() method which returns  IteratorProtocol object.   Second Approach Make the UserIdSequenceGenerator Confirm to IteratorProtocol itself and implement next(). We have used second approach in the above code. Finally the code in Action looks like seen below. var sequence = UserIdSequenceGenerator (startId

hitTest on iOS to identify the view being hit

Image
HitTest on iOS Use hitTest, to identify the view being tapped. The below method allows us to identify the view being selected. We can use it, to identify the view from which we would like to hide the touch events or identify the view we are interested into. Example: In the below case when we tap on Black View we can show an alert like "Black View tapped!" , if we tap on Red View , we can show an alert  "You are not allowed to tap on Red View" .  override func hitTest ( _ point: CGPoint , with event: UIEvent ?) -> UIView ? { } Here is a simple implementation of the method, to identify the view being hit. View hierarchy being used is listed below:- Blue(parent) - Red(child) - Green(child) - Yellow (child) - Orange (child) - Black (child) - White (child) So lets' start with override of hitTest method.      override func hitTest ( _ point: CGPoint , with event: UIEvent ?) -> UIView ? {         //1         if let viewAvailable = findPoin

SwiftUI Data Binding with the help of @State and @Binding

Image
Data Binding is an approach which binds the data source (i.e provider) to a consumer and vice-versa , so as to keep them synchronized. UI Data binding is something which we will be interested to learn along with SwiftUI. UI Data binding is a design pattern to simplify the development of GUI applications. The name itself gives us an idea about the data will be binding to the View and vice-versa. That is , when data updates the view updates or when the view updates so does the data gets updated. Just to take as an example, there are many javascript framework which support them example ReactNative or Angular JS Framework. What would be the traditional approach? UITextField changes will be identified using the UITextFileDelegate or add Target to get notified when UIControlEventEditingChanged. Once we receive the notification we update the object. Seems like it will be too much of code to handle such behavior. Lately with iOS 13 SwiftUI , we are now able to take advantage of this design p