4.9 Functions can have multiple return values [Swift original tutorial]

Original: http://coolketang.com/staticCoding/5a992500a22b9d00443914e9.html

1. We can use a tuple type as the return type of the function to return a tuple with multiple values ​​as the return value of the function. 



2. Create a function that has a tuple as the return type. A tuple is a set of comma-separated values ​​enclosed in parentheses. 


3. Define three variables with an initial value of 0 in turn to store vowels, consonants and other letters. 


4. Add a loop statement to iterate over all characters in a string. 


5. Use conditional statements to judge each character. Before judging, first convert the character to a string, and then call the lowercase method to convert the letter to the lowercase style. 


6. When a letter in the string is a vowel, increment the vowel variable by 1. 


7. When a letter in the string is a consonant, increment the consonant variable by 1. 


8. When the letters in the string are not vowels and consonants, increment the value of the third variable by 1. 


9. At the end of the function, return a tuple of three variables. 


10. Finally call the function. As can be seen from the right side of the code in line 25, the number of vowels is 3 and the number of consonants is 7. 


This article is organized from: "Swift4 Interactive Tutorial", the real [Hands-on] teaching mode, get started with iOS development and Swift language at the fastest speed, free download from Apple Store App Store: https://itunes.apple.com/cn/app/id1320746678 , or scan the QR code at the bottom of this page . Course supporting material download address: data download


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325522311&siteId=291194637