iOS移動(dòng)應(yīng)用程序開發(fā) | 第四講-函數(shù)和枚舉:Reusing Code ...

第四次作業(yè)
作業(yè)一:完成Playgrounds App《學(xué)習(xí)編程2》第4、5章
作業(yè)二:完成下面的練習(xí)題
1. Write a billSplit function to help calculate how to evenly split a dining bill. The function should take parameters for the total cost of the meal (a Double), a tip percentage (an Int), and a number of diners (an Int). It should print how much each diner will need to pay.
2. Write a better version of the billSplit function to help calculate how to evenly split a dining bill. This time, the function should have sensible defaults for some of the values: 2 diners, 20% tip. It should still print how much each diner will need to pay.
3 (i) Create a new enumeration to represent movie genres. It should contain a handful of movie genres, of your choice Once you’ve created it, make a switch statement, and switch on a variable storing a genre (maybe call it favoriteGenre?). Print out a sassy comment for each genre.?
(ii) With that working, try creating a new enumeration that represents cocktails. For each cocktail, store a raw string value describing the cocktail. With that done, iterate through the cases of the enum using a loop, and print the description of each cocktail.
4 (i) Write a closure that takes a single integer as its parameter, and returns that integer multiplied by itself inside a string.
(ii) Write some code to use that closure, by passing in the value 10 (it should print "The result is 100" or similar).
(iii) Now, update the closure and make it take two integers as parameters. Test that closure with 10 and 50 (it should return "The result is 500" or similar).
5. BE the Swift Compiler
Each of the Swift code snippets below represents something about closures. Your job is to play Swift compiler, determine whether each one is valid, and explain why.


