大佬教程收集整理的这篇文章主要介绍了ios – 任意操作可选择快速链接?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
class Person { var residence: Residence? } class Residence { var numberOfRooms = 1 } let john = Person() if let roomCount = john.residence?.numberOfRooms { println("John's residence has \(roomCount) room(s).") } else { println("Unable to retrieve the number of rooms.") }
想象一下尝试用一些算术运算来调整条件.这会导致编译器错误,因为模运算符不支持选项.
if john.residence?.numberOfRooms % 2 == 0 { // compiler error: Value of optional type int? not unwrapped println("John has an even number of rooms") } else { println("John has an odd number of rooms") }
当然,您总是可以执行以下操作,但它缺乏可选链接的简单性和简洁性.
if let residence = john.residence { if residence.numberOfRooms % 2 == 0 { println("John has an even number of rooms") }else{ println("John has an odd number of rooms") } } else { println("John has an odd number of rooms") }
以上是大佬教程为你收集整理的ios – 任意操作可选择快速链接?全部内容,希望文章能够帮你解决ios – 任意操作可选择快速链接?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。