Posts

Showing posts with the label Mutating Method Requirement

Enumeration in swift

Enumerations An  enumeration  defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code. enum Direction { case north case south case east case west } Note : Unlike C and Objective C, Here enum is not initialized with a default value.  The name must start with a Capital letter. var direction = Direction. west or var direction = . west If the  case  for  .west  is omitted, this code does not compile, because it does not consider the complete list of  CompassPoint  cases. When it is not appropriate to provide a  case  for every enumeration case, you can provide a  default  case to cover any cases that are not addressed explicitly: switch somePlanet { case . earth : print ( "Mostly harmless" ) default : print ( "Not a safe place for humans" ) } Associated Values ...

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 } }         pro...