温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Swift协议怎么使用

发布时间:2022-08-24 10:45:02 来源:亿速云 阅读:202 作者:iii 栏目:开发技术

这篇“Swift协议怎么使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Swift协议怎么使用”文章吧。

协议(Protocol)

1、协议可以用来定义方法、属性、下标的声明,协议可以被枚举、结构体、类遵守(多个协议之间用逗号隔开)

protocol Drawable {     func draw()     var x: Int { get set }     var y: Int { get }     subscript(index: Int) -> Int { get set } }

2、协议中定义方法时不能有默认参数值

3、默认情况下,协议中定义的内容必须全部都实现

协议中的属性

1、协议中定义属性时必须用var关键字

2、实现协议时属性权限要不小于协议中定义的属性权限

协议定义get、set,用var存储属性或get、set计算属性去实现

协议定义get,用任何属性都可以实现

class Person: Drawable {     var x: Int = 0     let y: Int = 0     func draw() {         print("Person draw")     }     subscript(index: Int) -> Int {         set {}         get { index }     } }
class Person: Drawable {     var x: Int {         get { 0 }         set {}     }     var y: Int {         get { 0 }     }     func draw() {         print("Person draw")     }     subscript(index: Int) -> Int {         set {}         get { index }     } }

static、class

1、为了保证通用,协议中必须用static定义类型方法、类型属性、类型下标

protocol Drawable {     static func draw() } class Person1: Drawable {     class func draw() {         print("Person1 draw")     } } class Person2: Drawable {     static func draw() {         print("Person2 draw")     } }

mutating

1、只有将协议中的实例方法标记为mutating

才允许结构体、枚举的具体实现修改自身内存

类在实现方法时不用加mutating,枚举、结构体才需要加mutating

protocol Drawable {     mutating func draw() } class Size: Drawable {     var width: Int = 0     func draw() {         width = 10     } } static Point: Drawable {     var x: Int = 0     mutating func draw() {         x = 10     } }

init

1、协议里面还可以定义初始化器init

非final类实现时必须加上required

protocol Drawable {     init(x: Int, y: Int) } class Point: Drawable {     required init(x: Int, y: Int) {              } } final class Size: Drawable {     init(x: Int, y: Int) {              } }

2、如果从协议实现的初始化器,刚好是重写了父类的指定初始化器

那么这个初始化必须同时加required、override

protocol Liveable {     init(age: Int) } class Person {     init(age: Int) {} } class Student: Person, Liveable {     required override init(age: Int) {         super.init(age: age)     } }

init、init?、init!

1、协议中定义的init?、init!,可以用init、init?、init!去实现

2、协议中定义的init,可以用init、init!去实现

protocol Liveable {     init()     init?(age: Int)     init!(no: Int) } class Person: Liveable {     required init() {} //    required init!() {}     required init?(age: Int) {} //    required init!(age: Int) {} //    required init(age: Int) {}     required init!(no: Int) {} //    required init?(no: Int) {} //    required init(no: Int) {} }

协议的继承

1、一个协议可以继承其他协议

协议组合

1、协议组合,可以包含1个类类型(最多1个)

protocol Livable {} protocol Runnable {} class Person {} //接收Person或者其子类的实例 func fn0(obj: Person) {} //接收遵守Livable协议的实例 func fn1(obj: Livable) {} //接收同时遵守Livable和Runnable协议的实例 func fn2(obj: Livable & Runnable) {} //接收同时遵守Livable、Runnable协议,并且是Person或者其子类的实例 func fn3(obj: Person & Livable & Runnable) {}
typealias RealPerson = Person & Livable & Runnable func fn4(obj: RealPerson) {}

CaseIterable

1、让枚举遵守CaseIterable协议,可以实现遍历枚举值

enum Season: CaseIterable {     case spring, summer, autumn, winter } let seasons = Season.allCases print(seasons.count) // 4 for season in seasons {     print(season) }

CustomStringConvertible

1、遵守CustomStringConvertible协议,可以自定义实例的打印字符串

class Person: CustomStringConvertible {     var age: Int     var name: String     init(age: Int, name: String) {         self.age = age         self.name = name     }     var description: String {         "age = \(age), name = \(name)"     } } var p = Person(age: 10, name: "Jack") print(p) // age = 10, name = Jack

以上就是关于“Swift协议怎么使用”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI