According to Swift document, a switch statement is:
A switch statement considers a value and compares it against several possible matching patterns. It then executes an appropriate block of code, based on the first pattern that matches successfully. A switch statement provides an alternative to the if statement for responding to multiple potential states.
Basiclly speaking, in a switch statement, it will try all possible values for a variable. Also, a switch statement must have a default value.
// Switch on the size Int. switch size { case0, 1: // Two values match Small. print("Small") case2, 3: // Two values match Medium. print("Medium") case4, 5: // Two values match Large. print("Large") default: break }
Ranges Example
Output is Upper half for the example below.
Swift program that uses ranges in cases
1 2 3 4 5 6 7 8 9 10 11
let code =70
// Switch based on ranges. switch code { case0...50: print("Lower half") case51...100: print("Upper half") default: print("Invalid") }
Fallthrough Example
Fallthrough means that control proceeds to the next case in a switch, and the next case is entered even if the value does not match.
Output is below for the following example.
Number contains 2
Number contains 1
Number contains 0
``` swift let size = 2 // Use switch with fallthrough statements. switch size { case 3: // This case will execute statements in case 2, 1 and 0. print("Number contains 3") fallthrough case 2: print("Number contains 2") fallthrough case 1: print("Number contains 1") fallthrough case 0: print("Number contains 0") default: print("Invalid") }
### String Example
Output is `Is cat` for the example below.
>Swift program that uses switch on string
1 2 3 4 5 6 7 8 9 10 11 12 13
let name ="cat"
// Switch on the name string. switch name { case"bird": print("Is bird") case"dog": print("Is dog") case"cat": print("Is cat") // This is printed. default: print("Something else") }
### Where Example
Where method can do further checking in some cases. For instance, we want to print out a tuple argument with a integer greater than 10. The following code output is below.
1 2
Default Number = 15, Letter = b
>Swift program that uses where in switch case
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
functest(code: (Int, Character)) { // Switch on the tuple argument. // We use let to allow the tuple items to be referenced. // We use where to test a part of the tuple. switch code { caselet(number, letter) where number >=10: print("Number = \(number), Letter = \(letter)") default: print("Default") } }
// Call test with a tuple argument. test(code: (5, "a")) // Call test again. test(code: (15, "b"))
### Tuple Example
We use a "tuple pattern" to match all items in the tuple.
Output is `Is xyz200` for the example below.
>Swift that uses tuple switch
1 2 3 4 5 6 7 8
let data = ("xyz", 200)
// Match complete tuple values. switch (data) { case ("abc", 300): print("Is abc300") case ("xyz", 200): print("Is xyz200") default: print("Not known") }
### Let Values
A case block can capture values in a tuple switch. We use the "let" keyword and provide an identifier.
Output is `Monkey has size 200` for the example below.
>Swift that uses let, value switch
1 2 3 4 5 6 7
let value = ("monkey", 200)
// Use let to capture a variable in a tuple. switch (value) { case ("monkey", let size): print("Monkey has size \(size)") default: break }
### Tuples With Any Value
With an underscore, we can match just parts of a tuple in a switch.
Output is `Second value is 1` for the example below.
>Swift that switches, matches any tuple value
1 2 3 4 5 6 7 8 9
let tuple = ("cat", 1, "penguin")
// Switch on tuple. // ... Match second value of the tuple. switch (tuple) { case (_, 1, _): print("Second value is 1") case (_, 2, _): print("Second value is 2") default: print("No case") }
### Reference
* [Control Flow](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ControlFlow.html)
* [Switch in Swift](https://www.dotnetperls.com/switch-swift)
---
*This is the end of post*
A computer program is said to learn from experience E with respect to some class of tasks T and performance measure P, if its performance at tasks in T, as measured by P, improves with experience E.
Example: playing checkers.
E = the experience of playing many games of checkers
T = the task of playing checkers
P = the probability that the program will win the next game.
In general, any machine learning problem can be assigned to one of two broad classifications: Supervised learning and Unsupervised learning.
Supervised Learning
In supervised learning, we are given a data set and already know what our correct output should look like, having the idea that there is a relationship between the input and the output.
Supervised learning problems are categorized into “regression” and “classification” problems.
Regression problems
In a regression problem, we are trying to predict results within a continuous output, meaning that we are trying to map input variables to some continuous function.
Regression - Given a picture of a person, we have to predict their age on the basis of the given picture.
Classification problems
In a classification problem, we are instead trying to predict results in a discrete output. In other words, we are trying to map input variables into discrete categories.
Classification - Given a patient with a tumor, we have to predict whether the tumor is malignant or benign.
Unsupervised Learning
Unsupervised learning allows us to approach problems with little or no idea what our results should look like. We can derive structure from data where we don’t necessarily know the effect of the variables. We can derive this structure by clustering the data based on relationships among the variables in the data. With unsupervised learning there is no feedback based on the prediction results.
Clustering
Take a collection of 1,000,000 different genes, and find a way to automatically group these genes into groups that are somehow similar or related by different variables, such as lifespan, location, roles, and so on.
Non-clustering
The “Cocktail Party Algorithm”, allows you to find structure in a chaotic environment. (i.e. identifying individual voices and music from a mesh of sounds at a cocktail party).
它是 WD 4TB My Cloud Personal Network Attached Storage,是一款 NAS 设置。你可以把它理解成一个「不能移动」的大容量硬盘,同时也可以作为整个家庭的娱乐终端,还可以作为一台没有显示器的微型电脑。我是在 Amazon 上购买,价格是 $170,附上购买链接。
将它作为家庭影院的 Hub。在家中因为使用同一个 WiFi,这使得你可以随时方便地访问 My Cloud 上的数据。下载 VLC 播放器,你可以在任何移动设备 (iPhone, iPad, Android) 上播放 My Cloud 里面存放的电影。如果你使用智能电视或者 Apple TV 的话,你也可以下载 VLC 的应用程序,这样就可以直接在电视上播放 My Cloud 里的电影。
将它作为一个随时随地都可以访问的云端硬盘。下载 My Cloud 的app,或者登陆 http://www.mycloud.com/ 就可以在任何设备或者电脑上直接访问你所上传的资源,也可以下载到任何电脑上,同时还可以分享给你的朋友。
I would like to send a POST request to an API with two parameters. How to start with it?
The API is https://www.google.com/
The parameters are
1 2
id = 1234 name = wei
Answer
Below is the example for Swift 3, you can follow with that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
var request =URLRequest(url: URL(string: "https://www.google.com/")!) request.httpMethod ="POST" let postString ="id=1234&name=wei" request.httpBody = postString.data(using: .utf8) let task =URLSession.shared.dataTask(with: request) { data, response, error in guardlet data = data, error ==nilelse { print("error=\(error)") return }
iflet httpStatus = response as?HTTPURLResponse, httpStatus.statusCode !=200 { print("statusCode should be 200, but is \(httpStatus.statusCode)") print("response = \(response)") }