protocol SomeProtocol {
// 这里是协议的定义部分
}
//拥有父类的类在遵循协议时,应该将父类名放在协议名之前
class SomeClass: SomeSuperClass, FirstProtocol, AnotherProtocol {
// 这里是类的定义部分
}
var
来声明变量属性,还必须指明是只读的还是可读可写的,用{ set get }
来表示属性是可读可写的,只读属性则用{ get }
来表示protocol ClassAProtocol {
var str: String {get set} // 继承他的时候不一定非要是计算型属性
var str1: String {get} // 只读
func log() -> String
}
class ClassA: ClassAProtocol {
var str = "000"
var str1 = "111"
func log() -> String {
return str
}
}
var a: ClassA = ClassA()
print(a.log())
var ap: ClassAProtocol = a //ClassAProtocol类型可以存放其子类
a.str = "666" // 不报错,因为a是可读可写的
ap.str1 = "888" // 报错,a因为a不可读
mutating
关键字mutating
方法时,若是类类型,则不用写 mutating
关键字。而对于结构体和枚举,则必须写 mutating
关键字protocol ClassAProtocol{
var str:String{get set}
}
struct ClassA:ClassAProtocol{
var str: String = "000"
mutating func change(value:String) {
str = value
}
}
let a = ClassA.init()
a.str
required
修饰符protocol ClassAProtocol{
init(a:Int)
}
class ClassA:ClassAProtocol{
required init(a: Int) {
print(a)
}
}
let a = ClassA.init(a: 3)
protocol ClassAProtocol{
init()
}
class ClassA{
init() {
}
}
class ClassB:ClassA,ClassAProtocol{
// 因为遵循协议,需要加上 required
// 因为继承自父类,需要加上 override
required override init() {
}
}
protocol ClassProtocol {
func log() -> String
}
class ClassA:ClassProtocol{
func log() -> String {
return "classA"
}
}
class ClassB:ClassProtocol{
func log() -> String {
return "classB"
}
}
struct ClassC{
var pro:ClassProtocol //协议作为属性
func run() -> String {
return self.pro.log()
}
}
let c = ClassC.init(pro: ClassA())
c.run() //classA
protocol DosomethingProtocol {
func buy()
func share()
}
import UIKit
class APP: NSObject,DosomethingProtocol {
func buy() {
print("buy")
}
func share() {
print("share")
}
}
---------------------
import UIKit
class Student: NSObject {
var delegate:DosomethingProtocol?
}
-----------------------
let stu = Student()
let app = APP()
stu.delegate = app
stu.delegate!.share()
protocol DosomethingProtocol {
func buy()
func share()
}
extension APP:DosomethingProtocol{
func buy() {
print("buy")
}
func share() {
print("share")
}
}
import UIKit
class APP: NSObject {
var name:String?
func study() {
print("study")
}
}
protocol DosomethingProtocol {
func buy()
func share()
}
protocol PlayProtocol:DosomethingProtocol {
func play()
}
extension APP:PlayProtocol{
func buy() {
print("buy")
}
func share() {
print("share")
}
func play() {
print("Play")
}
}
import UIKit
class APP: NSObject {
var name:String?
func study() {
print("study")
}
}
let app = APP.init()
app.name = "app"
app.study()
app.share()
app.buy()
app.play()
在协议的继承列表中,通过添加 class
关键字来限制协议只能被类类型遵循,而结构体或枚举不能遵循该协议。class
关键字必须第一个出现在协议的继承列表中,在其他继承的协议之前
类型专属协议.png
需要同时遵循多个协议,可以将多个协议采用 Protocol & Protocol
这样的格式进行组合,称为 协议合成
protocol PlayProtocol{
func playGame()
}
protocol StudyProtocol {
func study()
}
class Boy:PlayProtocol,StudyProtocol{
func playGame() {
print("play")
}
func study() {
print("study")
}
func log(info:PlayProtocol&StudyProtocol) {
self.playGame()
self.study()
}
}
let boy = Boy()
boy.log(info: boy)
//play
//study
protocol PlayProtocol{
func playGame()
}
class Stu1:PlayProtocol{
func playGame() {
print("play")
}}
class Stu2:PlayProtocol{
func playGame() {
print("play")
}
}
class Stu3{
}
let stu1 = Stu1()
let stu2 = Stu2()
let stu3 = Stu3()
stu1 is PlayProtocol //true
stu2 is PlayProtocol //true
stu3 is PlayProtocol
stu1 as? PlayProtocol //true
stu2 as? PlayProtocol //true
stu3 as? PlayProtocol //nil
@objc
属性optional
关键字作为前缀来定义可选要求@objc protocol PlayProtocol{
func playGame()
@objc optional func run()
}