Answer:
- Protocol is a set of Property and Method declarations which are expected to be implemented in the adopting class.
Answer:
- No, protocol just contains method declarations.
protocol AProtocol {
// You can delare property without assigning value
var initialValue: Int { get set }
// You can make read-only property
var getOnly: Int { get }
// You can have function without defining its body
func add()
func subtract()
}
struct Calculator: AProtocol {
var initialValue: Int
var getOnly: Int
func add() {
}
func subtract() {
}
}
Answer:
- Yes. To provide default implementations, we can extend protocols.
protocol AProtocol {
// You can delare property without assigning value
var initialValue: Int { get set }
// You can make read-only property
var getOnly: Int { get }
// You can have function without defining its body
func add()
func subtract()
}
// Through extension, you can define properties and methods
extension AProtocol {
// You can assign values within extension
var initialValue: Int {
get {
return 0
}
set {
}
}
var getOnly: Int {
get {
return 0
}
}
// You can make body within extension
func add() {
print("Add")
}
}
struct Calculator: AProtocol {
// You must implement subtract() method because it doesn't have body
func subtract() {
}
}
Answer:
extension ObjectName: ProtocolName {
}
Answer:
- By making protocol extend AnyObject.
protocol AProtocol: AnyObject {
var initialValue: Int { get set }
func add()
}
// Compile Error
struct Calculator: AProtocol {
}
Answer:
- Use
- @objc optional var variableName: Int { get set }
- @objc optional func methodName()
- But this protocol is only available to class-type
Answer:
- To achieve multiple inheritance
- To achieve delegation
- To segregate specific feature methods into a single unit
protocol AProtocol {
var property1: String { get set }
func method1()
}
protocol BProtocol {
var property2: String { get set }
func method2()
}
struct aStruct: AProtocol, BProtocol {
var property1: String
func method1() {
}
var property2: String
func method2() {
}
}
Answer:
- Yes, by using protocol extensions we can provide default behavior for the protocol methods.
protocol AProtocol {
// You can delare property without assigning value
var initialValue: Int { get set }
// You can make read-only property
var getOnly: Int { get }
// You can have function without defining its body
func add()
func subtract()
}
// Through extension, you can define properties and methods
extension AProtocol {
// You can assign values within extension
var initialValue: Int {
get {
return 0
}
set {
}
}
var getOnly: Int {
get {
return 0
}
}
// You can make body within extension
func add() {
print("Add")
}
}
struct Calculator: AProtocol {
// You must implement subtract() method because it doesn't have body
func subtract() {
}
}
Answer:
- No, Protocols do not contain stored properties. We must specify the declared property is get only or get and set.
protocol AProtocol {
// You can delare property without assigning value
var initialValue: Int { get set }
}
// Through extension, you can define properties and methods
extension AProtocol {
// You can assign values within extension
var initialValue: Int {
get {
return 0
}
set {
}
}
}
Answer:
- By default, all methods in Protocol are required.
Answer:
- No, Optional variables are not expected to have a value in future. So to set or assign a value in future, the optional variable must be var.
Answer:
- Swift is Protocol Oriented Programming Language.
Section 3, Conditional Statement
Section 10, static type vs dynamic type
Section 15, higher order function
Section 19, protocols