You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
letsalih="Salih Kertik"letpaul="Paul Michel"letgeorge="George Harrison"letringo="Ringo Starr"letbeatles=[salih, paul, george, ringo]beatles[0]
//Be careful: Swift crashes if you read an item that doesn’t exist. For example, trying to read beatles[9] is a bad idea.
Sets
letcolors=Set(["red","green","blue"])
//If you try to insert a duplicate item into a set, the duplicates get ignored. For example:
letcolors2=Set(["red","green","blue","red","blue"])
//The final colors2 set will still only include red, green, and blue once.
//If you need a specific, fixed collection of related values where each item has a precise position or name, you should use a tuple:
letaddress=(house:555, street:"Taylor Swift Avenue", city:"Nashville")
//If you need a collection of values that must be unique or you need to be able to check whether a specific item is in there extremely quickly, you should use a set:
letset=Set(["aardvark","astronaut","azalea"])
//If you need a collection of values that can contain duplicates, or the order of your items matters, you should use an array:
letpythons=["Eric","Graham","John","Michael","Terry","Terry"]
letfavoriteIceCream=["Paul":"Chocolate","Sophie":"Vanilla"]
//We can read Paul’s favorite ice cream like this:
favoriteIceCream["Paul"]
//But if we tried reading the favorite ice cream for Charlotte, we’d get back nil, meaning that Swift doesn’t have a value for that key:
favoriteIceCream["Charlotte"]
//We can fix this by giving the dictionary a default value of “Unknown”, so that when no ice cream is found for Charlotte we get back “Unknown” rather than nil:
favoriteIceCream["Charlotte", default:"Unknown"]