Posts

Showing posts from 2016

Swift 3: Protocol Part 2

So moving onward from part 1 to explore more about the Protocol. Let's start it!! below mentioned example checked on Xcode 8.1 Protocol Property Requirements -  the protocol requires any conforming type to provide instance and type property with name and type. We can mention property as getter or setter or both types (Ex-1). Property needs to implement as per type to satisfies Protocol requirements.          Property requirement always needs to declare as  var . Property can be declared as  gettable  and  settable , {get}  and {get set} respectively. (Ex-1) protocol myProtocol {     var canBeSetAndGet : Int { get set }     var canBeGet : Int { get } } Type property can be prefixed with a class or static keyword when you declare in Protocol.       protocol myProtocol {     static var myProperty : String { get set } }         protocol myProtocol {     var myProperty : String { get set } } Protocol Method Requirements -  The protocol re

Swift 3: Protocol Part 1

Introduction: Protocol main and widely used concept in objective c and swift both. So here we will be discussing in swift. We have two parts for a protocol. Part -1, will have basic terminology and concept to understand it and Part 2 and Part 3 will have some more drills in detail about it. The protocol you can think of “Contract” between two bodies. If anyone has acquired it then, that has to follow certain rules to incorporate the action of the contract. Same way Protocol in objective will act in the same way. Protocols define the set of methods, properties, and required pieces of functionality. Any type whoever going to conform it will have to conform first and then implement the requirement. The protocol can be adopted by a class, struct, enum. Any type that fulfilling the requirement of that protocol is said to conform to that protocol. If you conform to the protocol then we need to implement the requirement. We can extend the protocol as well to extend the f

Swift - 3.0 : Range Operators

Range Operators:  In order to avoid traditional way of iterating through the range of values, Swift has introduced, shorthand, handy, Range Operators. Swift has two type of range operators by which you can iterate through the range of values. These are shorthand for to avoid traditional way of iterating through values using for loop etc. Traditional Iteration:  for (var i = 0 ; i<= 5 ;i += 1 ) {     // for iterating through values } Range Operator Type: Closed Range Operator  (a ... b) - range includes a and b. Half - Open Range Operator (a ..< b ) range includes an only. 1. Closed Range Operator - This operator defines as (x ... z) values from x to z. In this range of values 'x' and 'z' both are included in the range . But you should remember that value 'x' must not be greater than 'z' value. The compiler will give an error. If 'x' and 'z' are same then it will run successfully. Example: Just adding v

In Swift: NSBlockOperation and NSOperationQueue Sample

Let's first understand  Concurrency   which I am going to explain below:  Concurrency : It tells running several tasks in same time. So do some work on Main thread and on other thread at the same time, will come under concurrency. The iOS SDK and other way are there to achieve the same thing, but I have chosen  NSBlockOperation and NSOperationQueue . The  NSBlockOperation  class is a concrete subclass of  NSOperation  that manages the concurrent execution of one or more blocks. This is a very simple concept which I will explain in a very simple example.  There can be other use as well but I understood and used it in one scenario.  So, when you are doing some tasks, that tasks are taking time so you can use  NSOperationQueue   and NSBlockOperation queue if you don not want to involve Main Thread, as it is responsible for UI responsiveness. So if you are going to do a heavy operation  on Main Thread, which is taking time, user defiantly will stick in the UI