import Foundation func varProcessor(_ n: Any, intProcess: (Int) -> Void, strProcess: (String) -> Void) { let nInt = n as? Int ?? 0 // 如果n是整形数,nInt将会被赋予n的值 let nString = n as? String ?? "" // 如果n是字符串,nString将会被赋予n的值 switch n{ case is Int: // 如果n是整型,这里的代码将会被执行 intProcess(nInt) case is String: // 如果n是字符串,这里的代码将会被执行 strProcess(nString) default: print("An error occurred.") } } let test: [Any] = [0, 2, 0.2, "Hello"] for item in test { varProcessor(item, intProcess: {print("The value of n is \(String($0))")}, strProcess: {print("The length of n is \($0.count)")}) }