: Playground-noun:a place where people can play
Import Cocoa
class person {
var residence:residence? // Optional Connection
}
class Residence {
var rooms = [guest] ()
var numberofrooms:Int {
return rooms. Count
}
subscript(i:Int) ,
return Rooms I
}
func printnumberofrooms () {
println("The number of rooms is\(numberofrooms)")
}
var address:address?
}
class The hostel {
let name:String
Init (Name:String) {
Self. name = Name
}
}
class Address {
var buildingname:String?
var buildingnubmer:String?
var street:String?
func buildingidentifier (),String? {
if (buildingname ! =Nil) {
return Buildingname
}Elseif (buildingnubmer ! =Nil) {
returnBuildingnubmer
}Else {
return Nil
}
}
}
Let John =person ()
Let Johnshouse = Residence ()
Johnshouse.rooms[0] = Guest (name: "Living")
John.residence = Johnshouse
/*
You can join multi-layer alternative links to dig up The lower-level attribute methods and corner labels within the model. However, multi-tier alternative links can no longer add more layers than are returned to the selected value. This means :
If you try to obtain a type that is not an alternative type , because of the optional link it becomes the optional type. If the type you are trying to obtain is already an alternative type , it will not increase the availability because of the optional link. therefore :
If you try to get an int value by an optional link, no matter how many layers of links are used , the int is always returned ?. similarly , if you are trying to get an Int through an optional link ? Value , regardless of how many layer links are used to return the always Int?.
*/
Let johnsaddress =Address()
Johnsaddress. Buildingname ="the"
Johnsaddress. Street ="Laurel"
John. Residence!. Address =johnsaddress
// link the method of the return value for the selection
If let Buildingidentifier = John.residence?. Address?. Buildingidentifier ()?. uppercasestring {
println ("John ' s building identifier is \ (Buildingidentifier).")
//}
// connect multi-layer links
If let Johnsstreet = John.residence?. Address?. Street {
println ("John ' s street name is \ (johnsstreet).")
} else {
println ("Unable to retrieve the address.")
//}
// Use the optional link to call the corner label
If let Firstroomname = John.residence? [0].name {
println ("The first guest name is \ (firstroomname).")
} else {
println ("Unable to retrieve the first")
//}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
option selectable value (i)