Swift学習ノート
34679 ワード
1文字を文字列に結合
2 switchのデフォルトではbreakが追加されています.いくつかの特殊な使い方があります.
3関数のいくつかの高度な使い方
4閉パケットブロック宣言{(パラメータリスト)->戻り値タイプin return戻り値}
let catCharacters:[Character] = ["c","a","t","!"]
var catString = String(catCharacters)
catString = catString ?? " , "
print(catString)
let enclosedEAcute:Character = "\u{E9}\u{20DD}"
print(enclosedEAcute)
//======================================================================
// index
let hellomyfriends = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for index in hellomyfriends.indices {
print(hellomyfriends[index])
}
//do whle
repeat{
}while minutestInterval < 100
//======================================================================
//
//
if #available(iOS 12.0, *){
}else{
}
2 switchのデフォルトではbreakが追加されています.いくつかの特殊な使い方があります.
//
let somePoint = (1,1)
switch somePoint {
case (0,0):
FMprint("0,0")
// break , , ,
fallthrough
case (_,0):
FMprint("x,0")
// break , , ,
fallthrough
case (0,_):
FMprint("0,x")
// break , , ,
fallthrough
case(-2...2,-2...2):
FMprint("-2~2")
// break , , ,
fallthrough
default:
FMprint("default")
}
//======================================================================
// Where
let spoint = (1,2)
switch spoint {
case (let x,1):
FMprint("\(x),1")
case (1,let y):
FMprint("\(y),1")
case let (x ,y) where x == -y:
FMprint("x,y")
case (2,_):
FMprint("2,_")
case (let x,let y) where x == y:
FMprint("\(x),\(y)")
case (1,0),(1,2),(1,3):
FMprint("1,0,1,2,1,3")
default:
FMprint("default")
break
}
3関数のいくつかの高度な使い方
// ,inout ,a,b
self.swapTwoInts(&a1, &b1)
print("\(a1),\(b1)")
//
func swapTwoInts(_ a:inout Int,_ b: inout Int) {
let tempA = a
a = b
a = tempA
}
//======================================================================
//
let funs:(Int)->Int = self.chooseFuncsType(true)
print(funs(10))
//
func stepForward(_ input: Int) -> Int {
return input + 1
}
func stepBackward(_ input: Int) -> Int {
return input - 1
}
func chooseFuncsType(_ parm:Bool)->(Int)->Int{
return parm ? stepForward : stepBackward
}
//======================================================================
//
self.printResult(self.addtowNum(_:_:), 10, b: 14)
//
func printResult(_ function:(Int,Int)->Int,_ a:Int,b:Int) {
print("\(function(a,b))")
}
//======================================================================
//
let fun1:(Int,Int)->Int = self.chooseFunsFromType(true)
print(fun1(12,15))
//
func chooseFunsFromType(_ type:Bool)->(Int,Int)->Int{
func add(_ a:Int,_ b:Int)->Int{return a + b}
func multiplication(_ a:Int,_ b:Int)->Int{return a * b}
print(multiplication(1,2))
return type ? add : multiplication
}
4閉パケットブロック宣言{(パラメータリスト)->戻り値タイプin return戻り値}
let myblock = {(a:Int,b:Int)->Int in return a + b }
** **
//
func blockend(a:String,b:String,closure:(String,String)->String)->String{
return closure(a,b)
}
/*
, 。 ,
*/
//
blockend(a: "a",b:"b", closure: {(c1,c2)in return c1 + c2})
// return
blockend(a: "a",b:"b", closure: {(c1,c2)in c1 + c2})
//
blockend(a: "a",b:"b", closure: {$0 + $1})
//
blockend(a: "bh", b: "ch"){($0) + ($1)}
//======================================================================
** **
/* , , 。 , @escaping, “ ” 。 */
var completionHandlers: [() -> Void] = []
func someFunctionWithEscapingClosure(completionHandler: @escaping () -> Void) {
completionHandlers.append(completionHandler)
}
** **
/* , 。 , , 。 , 。*/
var customersInLine = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
print(customersInLine.count)
// "5"
let customerProvider = { customersInLine.remove(at: 0) }
print(customersInLine.count)
// "5"
print("Now serving \(customerProvider())!")
// Prints "Now serving Chris!"
print(customersInLine.count)
// "4"
/* ,customersInLine , , 。 , , 。 ,customerProvider String, () -> String, String 。*/
/* serve(customer:) 。 serve(customer:) , , @autoclosure 。 String ( ) 。customerProvider , @autoclosure 。*/
// customersInLine is ["Ewa", "Barry", "Daniella"]
func serve(customer customerProvider: @autoclosure () -> String) {
print("Now serving \(customerProvider())!")
}
serve(customer: customersInLine.remove(at: 0))