Swift Protocols
Protocols specify the methods and properties required to implement a particular function.\\n\\nAny type that satisfies the requirements of a protocol is said to conform to that protocol.\\n\\nClasses, structures, or enumerations can conform to protocols, and provide concrete implementations to complete the methods and functions defined by the protocol.\\n\\nSwift Protocols are blueprints that define methods, properties, and other requirements for a specific task or function. Protocols can be adopted by classes, structures, and enumerations, which provide the concrete implementations of these requirements.\\n\\nProtocols are a very powerful feature in the Swift language, allowing developers to define interfaces that specify the properties and methods a type must have, without specifying the concrete implementation of those properties and methods.\\n\\n### Syntax\\n\\nThe syntax format of a protocol is as follows:\\n\\nprotocol SomeProtocol { // Here you can define protocol requirements var someProperty: Int { get set } func someMethod()}\\nSomeProtocol defines a read-write property someProperty and a method someMethod.\\n\\nTo make a class conform to a protocol, add the protocol name after the type name, separated by a colon :, as part of the type definition. When conforming to multiple protocols, separate them with a comma ,.\\n\\nstruct SomeStructure: FirstProtocol, AnotherProtocol { // Struct content}\\nIf a class conforms to a protocol while also having a superclass, the superclass name should be placed before the protocol name, separated by a comma.\\n\\nclass SomeClass: SomeSuperClass, FirstProtocol, AnotherProtocol { // Class content}\\n\\n* * *\\n\\n## Property Requirements\\n\\nProtocols are used to specify specific instance properties or class properties, without specifying whether they are stored or computed properties. Additionally, it must be indicated whether they are read-only or read-write.\\n\\nIn protocols, var is typically used to declare variable properties, and { set get } is added after the type declaration to indicate that the property is read-write, while read-only properties are indicated with { get }.\\n\\n## Instance\\n\\nprotocol ClassA {\\n\\n var marks: Int { get set }\\n\\n var result: Bool { get }\\n\\nfunc attendance()-> String\\n\\n func markssecured()-> String\\n\\n}\\n\\nprotocol ClassB: ClassA {\\n\\n var present: Bool { get set }\\n\\n var subject: String { get set }\\n\\n var stname: String { get set }\\n\\n}\\n\\nclass ClassC: ClassB {\\n\\n var marks =96\\n\\n let result =true\\n\\n var present =false\\n\\n var subject ="Swift Protocol\\n\\n var stname ="Protocols"\\n\\nfunc attendance()-> String {\\n\\nreturn"The \\\\(stname) has secured 99% attendance"\\n\\n}\\n\\nfunc markssecured()-> String {\\n\\nreturn"\\\\(stname) has scored \\\\(marks) marks"\\n\\n}\\n\\n}\\n\\nlet studdet = ClassC()\\n\\n studdet.stname="Swift"\\n\\n studdet.marks=98\\n\\nprint(studdet.markssecured())\\n\\n print(studdet.attendance())\\n\\n print("Marks: \\\\(studdet.marks)")\\n\\n print("Result: \\\\(studdet.result)")\\n\\n print("Present: \\\\(studdet.present)")\\n\\n print("Subject: \\\\(studdet.subject)")\\n\\n print("Student Name: \\\\(studdet.stname)")\\n\\nThe output of the above program execution is:\\n\\nSwift has scored 98 marks The Swift has secured 99% attendance Marks: 98Result: truePresent: falseSubject: Swift Protocol Student Name: Swift\\n\\n* * *\\n\\n## Mutating Method Requirements\\n\\nSometimes it is necessary to modify its instance within a method.\\n\\nFor example, in instance methods of value types (structures, enumerations), the mutating keyword is used as a prefix to the function, written before func, indicating that the method can modify the instance it belongs to and its instance properties.\\n\\nprotocol Daysofaweek { mutating func show()}enum Days: Daysofaweek { case sun, mon, tue, wed, thurs, fri, sat mutating func show() { switch self { case .sun: self = .sun print("Sunday") case .mon: self = .mon print("Monday") case .tue: self = .tue print("Tuesday") case .wed: self = .wed print("Wednesday") case .thurs: self = .thurs print("Thursday") case .fri: self = .fri print("Friday") case .sat: self = .sat print("Saturday") } }}var res = Days.wed res.show()\\nThe output of the above program execution is:\\n\\nWednesday\\n\\n* * *\\n\\n## Initializer Requirements\\n\\nProtocols can require their conforming types to implement specified initializers.\\n\\nYou can write the initializer declaration in the protocol definition just like a normal initializer, but without writing the curly braces and the body of the initializer. The syntax is as follows:\\n\\nprotocol SomeProtocol { init(someParameter: Int)}\\n### Instance\\n\\nprotocol tcpprotocol { init(aprot: Int)}\\n\\n* * *\\n\\n## Implementing Protocol Initializer Requirements in Classes\\n\\nYou can implement the initializer in a class that conforms to the protocol, and specify it as either a designated initializer or a convenience initializer of the class. In both cases, you must mark the initializer implementation with the "required" modifier:\\n\\nclass SomeClass: SomeProtocol { required init(someParameter: Int) { // Initializer implementation }} protocol tcpprotocol { init(aprot: Int)}class tcpClass: tcpprotocol { required init(aprot: Int) { }}\\nUsing the required modifier ensures that all subclasses conforming to the protocol can also provide an explicit or inherited implementation for the initializer requirement.\\n\\nIf a subclass overrides a designated initializer of its superclass, and that initializer conforms to a protocol's requirement, then the implementation of that initializer needs to be marked with both the required and override modifiers:\\n\\nprotocol TcpProtocol { init(no1: Int)}class MainClass { var no1: Int // Local variable init(no1: Int) { self.no1 = no1 // Initialization }}class SubClass: MainClass, TcpProtocol { var no2: Int init(no1: Int, no2: Int) { self.no2 = no2 super.init(no1: no1) } // Because it conforms to a protocol, you need to add"required"; Because it inherits from a parent class, you need to add"override" required override convenience init(no1: Int) { self.init(no1: no1, no2: 0) }}let res = MainClass(no1: 20)let show = SubClass(no1: 30, no2: 50)print("res is: \\\\(res.no1)")print("show.no1 is: \\\\(show.no1)")print("show.no2 is: \\\\(show.no2)")\\nThe output of the above program execution is:\\n\\nres is: 20 show.no1 is: 30 show.no2 is: 50\\n\\n* * *\\n\\n## Protocols as Types\\n\\nAlthough a protocol itself does not implement any functionality, it can be used as a type.\\n\\nProtocols can be used just like any other ordinary type. Use cases:\\n\\n* As a parameter type or return type in functions, methods, or initializers\\n* As the type of a constant, variable, or property\\n* As the type of elements in an array, dictionary, or other container\\n\\n### Instance\\n\\nprotocol Generator { associatedtype Members func next() -> Members?}// Example code: Use iterators and `map` functions// Use the array's iterator var items = [10, 20, 30].makeIterator()while let x = items.next() { print(x)}// Use `map` functionsfor list in [1, 2, 3].map({ i in i * 5 }) { print(list)}// Directly print the array using print([100, 200, 300])print([1, 2, 3].map({ i in i * 10 }))\\nThe output of the above program execution is:\\n\\n10203051015[100, 200, 300][10, 20, 30]\\n\\n* * *\\n\\n## Adding Protocol Conformance with an Extension\\n\\nWe can extend existing types (classes, structures, enumerations, etc.) through extensions.\\n\\nExtensions can add members such as properties, methods, subscripts, and protocols to existing types.\\n\\nprotocol AgeClassificationProtocol { var age: Int { get } func agetype() -> String}class Person { let firstname: String let lastname: String var age: Int init(firstname: String, lastname: String, age: Int = 10) { self.firstname = firstname self.lastname = lastname self.age = age }} extension Person: AgeClassificationProtocol { func fullname() -> String { return "\\\\(firstname) \\\\(lastname)" } func agetype() -> String { switch age { case 0...2: return "Baby" case 3...12: // Corrected to return starting from 3 "Child" case 13...19: return "Teenager" case let x where x > 65: return "Elderly" default: return "Normal" } }}// Test code let person1 = Person(firstname: "John", lastname: "Doe", age: 25)let person2 = Person(firstname: "Jane", lastname: "Smith", age: 70)let person3 = Person(firstname: "Baby", lastname: "Yoda", age: 1)print("\\\\(person1.fullname()) is a \\\\(person1.agetype())")print("\\\\(person2.fullname()) is a \\\\(person2.agetype())")print("\\\\(person3.fullname()) is a \\\\(person3.agetype())")\\nThe output of the above code is:\\n\\nJohn Doe is a NormalJane Smith is a ElderlyBaby Yoda is a Baby\\n\\n* * *\\n\\n## Protocol Inheritance\\n\\nProtocols can inherit one or more other protocols, and can add new content requirements on top of the inherited protocols.\\n\\nThe syntax for protocol inheritance is similar to class inheritance, with multiple inherited protocols separated by commas:\\n\\nprotocol InheritingProtocol: SomeProtocol, AnotherProtocol { // Protocol definition}\\n### Instance\\n\\nprotocol Classa { var
YouTip