option selectable value (i)

Source: Internet
Author: User

: 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.