Swift(十七、可选链)

Swift(十七、可选链),第1张

概述1、Swift入门学习笔记(第一版),对Swift的基础知识点进行梳理总结。知识点一直在变,只是作为参考,以苹果官方文档为准~ 2、在学习完基本的知识点以后会结合官方文档及相关资料,在此版本的基础上进行添加更改。 十七、可选链 1、写于前-概念梳理 1.1、可选类型使用时需要解包,解包时需要判断是否为nil,常用if语句判断,再去访问其属性或方法,否则会运行错误。当可选类型访问其属性还是可选时,即

1、Swift入门学习笔记(第一版),对Swift的基础知识点进行梳理总结。知识点一直在变,只是作为参考,以苹果官方文档为准~

2、在学习完基本的知识点以后会结合官方文档及相关资料,在此版本的基础上进行添加更改。

十七、可选链 1、写于前-概念梳理

1.1、可选类型使用时需要解包,解包时需要判断是否为nil,常用if语句判断,再去访问其属性或方法,否则会运行错误。当可选类型访问其属性还是可选时,即多层可选,那么if就显得比较麻烦。Swift提供了可选链(可空链式调用)

1.2、可空链式调用:一种可以请求和调用属性、方法和下标的过程,请求和调用目标可能为空。不为空调用成功,为空返回空(nil)。多个连续调用链接在一起,有任意一个结点为空(nil)将导致整个链调用失败。

1.3、多层链接:
a、访问的值不是可空,通过可选链将会放回可空值
b、访问的值已经是可空,通过可选链调用不会变的“更”可空
简言之:
a、可选链访问一个Int值,将返回Int?
b、可选链访问Int?值,你该不会变的更可空(仍然是可空)

通过拆分官方文档来说明:

class Person {    var resIDence:ResIDence?}class ResIDence {    var address:Address?}class Address {    var buildingname:String?    var buildingNumber:String?    func buildingIDentifIEr() -> String? {        if buildingname != nil {            return buildingname        }else if buildingNumber != nil {            return buildingNumber        }else {            return nil        }    }}
2、可空链式调用访问属性
let Zane = Person()if let number = Zane.resIDence?.address?.buildingNumber {    print("The buildingNumber of Zane's resIDence is \(number)")}else {    print("Unable to get the buildingNumber")}

Output:

Unable to get the buildingNumber

可选链任意一个属性为空则返回nil

2.1、设置属性值
let myResIDence = ResIDence()Zane.resIDence = myResIDencelet someAddress = Address()someAddress.buildingNumber = "218"Zane.resIDence?.address = someAddressif let number = Zane.resIDence?.address?.buildingNumber {    print("The buildingNumber of Zane's resIDence is \(number)")}else {    print("Unable to get the buildingNumber")}

Output:

Output:The buildingNumber of Zane's resIDence is 218
2.2、访问函数

通过可空链来调用AddressbuildingIDentifIEr()方法,返回的是String?类型,因此打印的是可选类型

print(Zane.resIDence?.address?.buildingIDentifIEr())

Output:

Optional("218")
if let buildingIDentifIEr = Zane.resIDence?.address?.buildingIDentifIEr() {    print("Zane's building IDentifIEr is \(buildingIDentifIEr)")}

Output:

Zane's building IDentifIEr is 218
3、下标可空值访问
class Person {    var resIDence:ResIDence?}class ResIDence {    var rooms = [Room]()    var numberOfRooms:Int {        return rooms.count    }    subscript(i:Int) -> Room {        get {            return rooms[i]        }        set {            rooms[i] = newValue        }    }    func printNumberOfRooms() {        print("The number of rooms is \(numberOfRooms)")    }}

有一个存储Room类的数组,及房间数属性
通过下标读写数组元素

class Room {    let name:String    init(name:String) {        self.name = name    }}
3.1、可选链调用方法
let Zane = Person()if Zane.resIDence?.printNumberOfRooms() != nil {    print("It was possible to print the number of rooms")} else {    print("Unable to print the number of rooms")}

Output:

Unable to print the number of rooms
3.2、可选链访问下标

访问下标时,问号放在方括号前面
访问Zane.resIDencerooms数组中第一个房间名称

if let firstRoomname = Zane.resIDence?.rooms[0].name {//if let firstRoomname = Zane.resIDence?[0].name {      //等同    print("The first room name is \(firstRoomname).")} else {    print("Unable to get the first room name")}

Output:

Unable to get the first room name

定义属性

let myResIDence = ResIDence()myResIDence.rooms.append(Room(name: "living Room"))myResIDence.rooms.append(Room(name: "BathRoom"))Zane.resIDence = myResIDenceif let firstRoomname = Zane.resIDence?.rooms[0].name {    print("The first room name is \(firstRoomname).")} else {    print("Unable to get the first room name")}if Zane.resIDence?.printNumberOfRooms() != nil {    print("It was possible to print the number of rooms")} else {    print("Unable to print the number of rooms")}

Output:

The first room name is living Room.The number of rooms is 2It was possible to print the number of rooms
总结

以上是内存溢出为你收集整理的Swift(十七、可选链)全部内容,希望文章能够帮你解决Swift(十七、可选链)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1082807.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存