Posts

Showing posts from 2019

enum in Swift of Type String

enum in Swift of Type String Many a times we would like to use enum to store text value's or may be even better to store fixed messages. Like in below case ,  we are creating a enumeration called "StringMessages" and "raw Values" are defined to be of Type "String"     enum StringMessages : String {        case emptyText = "InputText is Empty"        case nonEmptyText = "Input Text is not Empty"    } The only requirement here is the Raw Value should be unique. Raw Values can be String , characters , integer or floating point number types. Raw Values of this enum can be accessed using StringMessages.emptyText.rawValue or StringMessages.nonEmptyText.rawValue.

Attributes in Swift @available

Image
Attributes in Swift In Swift Attributes are used to provide some kind of additional information to a declaration or a type. For example if I want a function to be just available for iOS 12 version onwards. The Swift compiler will give warnings in such cases and provides suggestion to fix it as shown below. You can download the example for it from GitHub How do we declare an attribute? Attribute declaration starts with " @ " symbol and can also arguments which is enclosed in parentheses. Arguments specify some more information on how it applies to the existing attribute declaration. @ attributename ( attributearguments ) Example:  Here we have used attribute available and also gave more information on how it applies to the declaration , in this case it's only for iOS platform and available from12.5 version onwards.     @available (iOS,introduced: 12.5)     func methodForiOS12_5_only () {           } Types of Attributes in Swift T

Decorator Design Patter Using Swift

Decorator Design Pattern It allows you to add behavior to an individual object. It helps to add such behavior changes, dynamically without affecting the behavior of the other objects from the same parent class. Decorator design pattern strong holds to Single Responsibility principle. i.e a Base concrete Object. It is somewhat similar to chain or responsibility design pattern (ex: UIViewController,UIView) where exactly one of the class handles the responsibility, ex: touch events to UITextField, the iOS infrastructure tries to find the responder who can respond to it, if you have implemented a delegate or a IBAction change event , it is directed to that root UIView -> UIViewController -> UIWindow -> UIApplication. In case of the Decorator Design pattern all classes handle the request which we will see in the example listed below. A simple example to understand with this would be creating a "Simple Coffee" object and "Coffee Latte". I will try

UITestCase Automation Testing with Xcode

Image
UITestCase using Xcode TestCases for UI helps to automate the testing. In case of iOS , it is subclass of XCTestCase , which is part of XCTest framework . The subclassed object can be usually considered a Test Suite. A Test Suite is nothing but a group of similar test classes intended to test a specific behavior. To understand more about the automation testing using Xcode , we will consider a simple example which will test the alert message being shown appropriately or not for each button being tapped. The code is available on GitHub .                 XCTestCase and life cycle of a test suite XCTestCase subclass objects has all the required methods to setup and run a test case. More precisely methods for test suite life cycle. a) class method setUp -> use this to initialize the inputs required , before the first test case executes. override class func setUp(){ } b) method setUp  -> executed every time before a test case runs overri

Closures in Swift

Closures in Swift Closures in simple terms is a block of code which can be called any time during the current flow of execution. Closures have access to variables and function in the scope where they are defined. We can consider a simple array of numbers and try to transform it into a map with closure. let numbers = [1,2,3] The map function considers input a closure , will start with most extensive version of it as shown below. Iteration 1----------------------------------------------- let newMap = numbers . map ({ (input: Int ) -> Array < Int > in     return Array (repeating:input, count: input) }) "in" is used to separate the arguments and return type from the body. Iteration 2----------------------------------------------- If we see the Closure is the only parameter of the function . We can omit parentheses. We just removed () . let newMap2 = numbers . map { (input: Int ) -> Array < Int > in     re

Enums in Swift

Enums in Swift "Enums" define a finite state of any entity. This entity could be a direction in a compass, name of planets, day's in a weekend, brands of vehicles and so on so forth. This behavior of defining a finite state is extended to the programming world of software languages in a more readable and understandable format for developers. ex: Swift In Swift the basic syntax would be as follows enum <name> {     case <case> } To express a enum of Days in  a week we can write the code as shown below. Here <name> is name of the enum , DaysInWeek and I have give a Type to It, which is  Integer. enum DaysInAWeek: Int {     case Sunday = 0     case Monday,Tuesday,Wednesday,Thursday,Friday     case Saturday      } To initialize a var with enum value Monday , it can be written as follows. let  monday =  DaysInAWeek . Monday Another way to initialize it is as follows. let  monday =  DaysInAWeek (raw