问题
As in the title, what is the difference between didMove(to view: SKView) and didMoveToView(view: SKView)?
I understand that didMoveToView is the method and that view is of type SKView in the older(?) version. I don't understand the deal with 'to view: SKView' being passed to the didMove method. Are 'to' and 'view' separate variables? Is 'to' part of 'didMove'? What is going on?
EDIT: I understand that they both accomplish the same thing - but why is 'to' in the area that parameters normally go? Is there a benefit to this? It seems to be unnecessary complication?
回答1:
The difference is only in the syntax.
The new Swift 3 syntax is didMove(to view: SKView)
In Swift:
Each function parameter has both an argument label and a parameter name.The argument label is used when calling the function; each argument is written in the function call with its argument label before it. The parameter name is used in the implementation of the function. By default, parameters use their parameter name as their argument label.
func someFunction(argumentLabel parameterName: Int) {
// In the function body, parameterName refers to the argument value
// for that parameter.
}
So , the to
in the swift 3 version is only the argument label.
Probably you have also seen function where there is no need to insert the parameterName.
If you don’t want an argument label for a parameter, write an underscore (_) instead of an explicit argument label for that parameter.
func someFunction(_ firstParameterName: Int, secondParameterName: Int) {
// In the function body, firstParameterName and secondParameterName
// refer to the argument values for the first and second parameters.
}
someFunction(1, secondParameterName: 2)
Hope it helps to better understand these modifications: you can find more details here
回答2:
Nothing!
didMove(to view: SKView)
is Swift 3 syntax.
didMoveToView(view: SKView)
is Swift 2.x syntax.
FYI many many other parameters and method namings changed from Swift 2 to Swift 3. A very good list of changes can be found here
usage
It's debated but to most people the Swift 3 syntax is more readable since you do didMove(to: xyzView)
while the Swift 2.x syntax is less readable when you do didMoveToView(xyzView)
来源:https://stackoverflow.com/questions/40580873/what-is-the-difference-between-didmoveto-view-skview-and-didmovetoviewview