​Quick is a powerful and intuitive development language for macOS, iOS, watchOS and tvOS....

Page 224

Swift

let student = Person(name: "Roshan", age: 19) print(student) When we run the above program using playground, we get the following result: Priya is 21 years old Rehan is 29 years old Roshan is 19 years old

Checking for Protocol Conformance Protocol conformance is tested by 'is' and 'as' operators similar to that of type casting. 

The is operator returns true if an instance conforms to protocol standard and returns false if it fails.

The as? version of the downcast operator returns an optional value of the protocol's type, and this value is nil if the instance does not conform to that protocol.

The as version of the downcast operator forces the downcast to the protocol type and triggers a runtime error if the downcast does not succeed.

import Foundation

@objc protocol rectangle { var area: Double { get } }

@objc class Circle: rectangle { let pi = 3.1415927 var radius: Double var area: Double { return pi * radius * radius } init(radius: Double) { self.radius = radius } }

@objc class result: rectangle { var area: Double init(area: Double) { self.area = area } }

212


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.