跳到主要內容

Function Type

In the previous article, we briefly touched upon function types. A function has a particular type, composed of the function's parameter types and its return type. The printMessage(_:) function, for example, is of type (String) -> (). Remember that () symbolizes Void, which is equivalent to an empty tuple.
Because every function has a type, it's possible to define a function that accepts another function as a parameter. The following example shows how this works.
The printMessage(_:with:) function accepts a string as its first parameter and a function of type (String) -> () as its second parameter. In the function's body, the function that we pass in is invoked with the message argument.
The example also illustrates how we can invoke the printMessage(_:with:) function. The myMessageconstant is passed in as the first argument and the printMessage(_:) function as the second argument. How cool is that?
It is also possible to return a function from a function. The next example is a bit contrived, but it illustrates what the syntax looks like.
The compute(_:) function accepts a boolean and returns a function of type (Int, Int) -> Int. The compute(_:) function contains two nested functions that are also of type (Int, Int) -> Intadd(_:_:)and subtract(_:_:).
The compute(_:) function returns a reference to either the add(_:_:) or the subtract(_:_:) function, based on the value of the addition parameter.
The example also shows how to use the compute(_:) function. We store a reference to the function that is returned by the compute(_:) function in the computeFunction constant. We then invoke the function stored in computeFunction, passing in 1 and 2, store the result in the result constant, and print the value of result in the standard output. The example may look complex, but it is actually easy to understand if you know what is going on.



func chooseStepFunction(backward: Bool) -> (Int) -> Int {
    func stepForward(input: Int) -> Int { return input + 1 }
    func stepBackward(input: Int) -> Int { return input - 1 }
    return backward ? stepBackward : stepForward
}
var currentValue = -4
let moveNearerToZero = chooseStepFunction(backward: currentValue > 0)
// moveNearerToZero now refers to the nested stepForward() function
while currentValue != 0 {
    print("\(currentValue)... ")
    currentValue = moveNearerToZero(currentValue)
}
print("zero!")

留言

這個網誌中的熱門文章

collection view part6 swift

preselect collectionview https://stackoverflow.com/questions/45610785/how-to-preselect-and-highlight-a-cell-from-a-uicollectionview https://hackernoon.com/uicollectionviewcell-selection-made-easy-41dae148379d if collectionview allow multiple selection remeber add this, cell item could be Deselect collectionView. selectItem (at: indexPath, animated: true , scrollPosition: . top ) https://stackoverflow.com/questions/15330844/uicollectionview-select-and-deselect-issue save indexpath userdefault https://stackoverflow.com/questions/34804294/how-to-save-nsindexpath-locally https://stackoverflow.com/questions/37001643/how-to-add-nsindexpath-to-nsuserdefaults-in-swift navigation presneted viewcontroller dismiss https://stackoverflow.com/questions/31205002/why-is-it-bad-practice-to-have-a-viewcontroller-dismiss-itself indexpath comparison         if selectIndexPath . contains (indexPath) {             cell. isSelect...

android java 小數點的坑

如果temp  = 12.5                         val newTemp = "%.1f".format(Tempvalue) 原本的寫法是這樣 在法語區 將newTemp.toFloat() 後會掛,因為法語地區的小數點是 , 逗號 所以沒法轉成 12.5  會變成 12,5 後來的解法 將拿到的數字硬轉成 12.5  其他就不管了 val newTemp = "%.1f" . format ( Locale . US , Tempvalue)

collection view part 5 swift

load plist data https://www.cnblogs.com/gongyuhonglou/p/6029340.html https://blog.csdn.net/sophieDJF/article/details/88247388 https://stackoverflow.com/questions/24045570/how-do-i-get-a-plist-as-a-dictionary-in-swift https://stackoverflow.com/questions/40436895/how-to-read-plist-without-using-nsdictionary-in-swift get specifig key in dictionary https://stackoverflow.com/questions/24640990/how-do-i-get-the-key-at-a-specific-index-from-a-dictionary-in-swift get specifig key with index https://stackoverflow.com/questions/31775724/swift-dictionary-access-via-index get collectionview selection https://stackoverflow.com/questions/21639394/how-to-select-item-in-collectionview-programmatically add button in uinavigationbar https://www.hackingwithswift.com/example-code/uikit/how-to-add-a-bar-button-to-a-navigation-bar child viewcontroller pass data to parent viewcontroller https://stackoverflow.com/questions/39285588/how-to-pass-data-from-child-to-parent-view-controller-in-sw...