Monday, June 16, 2014

Swift In Depth - Day IV

Classes and Objects

class NamedShape
{
var numbeOfSides:int = 0
var name : String
init (name:String)
{
self.name = name
}
func simpleDescription() -> String
{
return "this is a named shape with \(numberOfSides)"
}
}

inheritance example is below 

class Square: NamedShape
{
var sidelength:Double
var numberofsides:Int 
init (sideLength: Double, name :String)
{
self.sideLength = sideLength
super.init(name:name)
numberofsides = 4
}
func area()
{
return sideLength * sideLength
}
override func simpleDescription() -> String
{
return "A Square with \(numberofsides) side"
}
}


let test = Square(sideLength:10, name:"Square")
test.simpleDescription 

we can set getter and setters much similar to objective C 
This works much similar to the functions. An example is below 

clase EquilateralTriangle : Shape 
{
var sideLength = 0
init(sideLength: Int, name: String)
{
self.sideLength = sideLength 
super.init(sideLength:sideLength, name:name)
}

var perimeter: Double 
{
get 
{
return self.sidelength * 3 
}
set 
{
self.sidelength = newValue / 3.0 
}
}
override func simpleDescription -> String 
{
return "An equilateral triangle with sides of length \(side length)"
}
}

the newValue is an implicit name of new Value. 

Enumerations and Structures

we can user enum to create enumeration. In Swift, like how classes can have methods, enumeration also can have methods. An example is given below 

enum Rank: Int 
{
case Ace = 1
case Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten 
case Jack, Queen, King
func simpleDescription() -> String
{
switch self
{
case .Ace:
return "ace"
case .Jack:
return "Jack"
case .Queen:
return "Queen"
default:
return String(self.toRaw()) 
}
}
}

ket ace = Rank.ace
let aceRawValue = ace.toRaw()

one can use toRaw and fromRaw functions to convert between the raw value and the enumeration value. 

if let convertedRank = Rank.fromRaw(3)
{
let threeDescription = convertedRank.simpleDescription()
}

Structures in Swift share commonalities to its closest match, which is class. One important difference between Struct and Class is that structure is always copied when passed around while class is referenced. 

we can use keyword struct to define structure. 

struct Card
{
var rank: Rank
var suit: Suit
func simpleDescription() -> String
{
return "The \(rank.simpleDescription) of \(suite.simpleDescription)"
}
}

let threeOfSpades = Card (rank: .Three, suite: .Spades)
let threeOfSpadesDescription = threeOfSpadesDescription

Protocols And Extensions

we can use protocol to define a protocol 

protocol ExampleProtocol 
{
var simpleDescription:String{get}
mutating fund adjust()
}

class SampleClass: ExampleProtcol 
{
var simpleDescription : String = "A very simple Description"
var anotherProperty : Int = 78342
func adjust() 
{
simpleDescription += "  Now 100% adjusted"
}
}

var simpleClassObj = SimpleClass()
simpleClassObj.adjust()
let aDesc = simpleClassObj.simpleDescription


No comments:

Post a Comment