官术网_书友最值得收藏!

How it works...

A tuple is declared as a comma-separated list of the types it contains, within brackets, for example, (Int, String), as shown in the preceding code. The preceding function, normalisedStarRating, normalizes the rating and creates numberOfStars as the closest number of stars, and ratingString as a display string. These values are then combined into a tuple and returned by placing them, separated by a comma, within brackets as (numberOfStars, ratingString):

let ratingValueAndDisplayString = normalisedStarRating(forRating: 5, ofPossibleTotal: 10) 

Calling our function returns a tuple that we store in a constant called ratingValueAndDisplayString. We can access the tuple's components by accessing the zero-based numbered member of the tuple:

let ratingValue = ratingValueAndDisplayString.0 
print(ratingValue) // 3 - Use to show the right number of stars

let ratingString = ratingValueAndDisplayString.1
print(ratingString) // "3 Star Movie" - Use to put in the label

There is another way to retrieve the values out of a tuple, and it can be achieved as the value is assigned. By specifying a tuple of variable names, each value of the tuple will be assigned to the respective variable name. Consider the following example:

let (nextValue, nextString) = normalisedStarRating(forRating: 8, ofPossibleTotal: 10)
print(nextValue) // 4
print(nextString) // "4 Star Movie"
主站蜘蛛池模板: 黔西| 丹江口市| 博湖县| 延津县| 息烽县| 海阳市| 翁牛特旗| 广饶县| 佛教| 西林县| 鄂托克旗| 安宁市| 东山县| 建平县| 永仁县| 简阳市| 政和县| 灵石县| 巴林右旗| 泰和县| 巨野县| 武安市| 乌什县| 和顺县| 曲水县| 济南市| 延津县| 名山县| 湟源县| 新巴尔虎右旗| 阿勒泰市| 东宁县| 将乐县| 高邮市| 于田县| 大邑县| 江西省| 石阡县| 额济纳旗| 西丰县| 肥城市|