/**
1.可选链可替代强制解析
2.为可选链定义模型类
3.通过可选链调用属性
4.通过可选链调用方法
5.使用可选链调用子脚本
6.连接多层链接
7.链接自判断返回值的方法
nil)。如果自判断的目标有值,那么调用就会成功;相反,如果选择的目标为空,则这种调用将返回空。多次请求或调用可以被链接在一起形成一个链,如果任何一个节点为空将导致整个链失效
swift的自判断链和OC中的消息为空有些相像,但是swift可以使用在任意类型中,并且失败与否可以被检测到
*/
//可选链可替代强制解析
class
var residence: Residence?
}
class
var numberOfRoom = 1;
}
let john = Person();
.residence = Residence();
if let roomCount = john.residence?.numberOfRoom{
//判断如果residence存在则取回numberOfRoom的值
// print("John's residence has \(roomCount) room(s).");
}else{
// print("Unable to retrieve the number of rooms.")
}
//print(roomCount);
/**
residence为可选值,未赋值,默认初始化为nil
*/
class Person {
var residence: Residence?
init(residence: Residence) {
self.residence = residence;
}
}
class Residence {
var numberOfRoom = 1;
}
let john = Person(residence: Residence());
let roomCount = john.residence!.numberOfRoom;
print(roomCount);
//为可选链定义模型类
/**
可以使用可选链来多层调用属性,方法和子脚本。可以利用它们之间的复杂模型来获取更底层的属性,并检查是否可以成功获取此类底层属性
*/
class
let name: String;
init(name: String) {
self.name
}
}
class
var buildingName: String?;
var buildingNumber: String?;
var street: String?;
func buildingIdentifier() -> String? {
if buildingName != nil
return buildingName!;
else if buildingNumber != nil
return buildingNumber!;
else{
return "[nil]";
}
}
}
class
var residence: Residence2?;
}
class
var rooms = [Room2]();
var numberOfRooms: Int
return rooms.count;
}
subscript(i: Int) -> Room2
return rooms[i];
}
func
print("The number of rooms is \(numberOfRooms)");
}
var address: Address?;
}
//通过可选链调用属性
let john2 = Person2();
if let roomCount = john2.residence?.numberOfRooms {
print("John2's residence has \(roomCount) room(s).");
}else{
print("Unable to retrieve the number of rooms.");
}
//通过可选链调用方法
if john2.residence?.printNumberOfRooms() != nil {
print("It was possible to print the number of rooms.");
}else{
print("It was not possible to print the number of rooms.");
}
//通过可选链调用子脚本
/**
当使用可选链来获取子脚本的时候,应该将问号放在子标本括号的前面而不是后面,可选链的问号一般直接跟在自判断表达语句的后面
*/
let johnsHouse = Residence2();
.rooms.append(Room2(name: "Living Room"));
.rooms.append(Room2(name: "Kitchen"));
.residence = johnsHouse;
if let firstRoomName = john2.residence?[0].name
print("The first room name is \(firstRoomName)");
}else{
print("Unable to retrieve the first room name");
}
//连接多层连接
/**
可以将多层可选链接连接在一起,可以掘取模型内更下层的属性方法和子脚本。然而多层可选链不能再添加比已经返回的可选值更多的层。也就是说:如果试图获得的类型不是可选类型,由于使用了可选链它将变成可选类型。如果你试图获得的类型已经是可选类型,由于可选链它也不会提高自判断性。
Int值,不论使用了多少层链接返回的总是Int?。相似的,如果试图通过可选链获得Int?值,不论使用了多少层链接返回的总是Int?.
*/
let johnsAddress = Address();
.buildingName = "The Larches";
.street = "Laurel Street";
.residence!.address = johnsAddress;
if let johnsStreet = john2.residence?.address?.street {
print("John2's street name is \(johnsStreet).");
}else{
print("Unable to retrieve the address.");
}//"!"在定义address实例时使用(john2.residence.address)。john2.residence属性是一个可选类型,因此需要在它获取address属性之前使用!解析以获得它的实际值
//链接自判断返回值的方法
/**
可以通过调用返回可选类型值的方法并按需链接方法的返回值
*/
if let buildingIdentifier = john2.residence?.address?.buildingIdentifier() {
print("John2's building identifier is \(buildingIdentifier).");
}
//如果还想进一步对方法返回值执行可选链,将可选链问号符放在方法括号的后面
/**
将可选链问号符放在括号后面是因为想要链接的可选值是方法的返回值,而不是方法本身
*/
if let upper = john2.residence?.address?.buildingIdentifier()?.uppercaseString
print("John's uppercase building identifier is \(upper).");
}