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