import Foundation
print("Hello, World!")
let tom = Person(name:"Tom", age:20)
print(tom.name)
//tom.walk
tom.walk()
tom.sleep(sleeptime: 36000)
let tomdes = tom.tech(a: "China", b: "Japan", c: 900)
print(tomdes)
let tomeleg = tom.armleg(arms: 20, legs: 20)
print(tomeleg)
//sets
var (legx,legy) = (22,30)
print(legx)
print(legy)
//array
let arraya = [23,3,343]
print(arraya[1])
//dictionary
let dicta:[String:Any] = ["keya":"litao","keyb": 33,"keyc": 120]
print(dicta["keya"] as Any)
//dictionary
let dictb:[Int:Int] = [2:20, 3:30, 4:40]
print(dictb[3] as Any)
Person.swift
import Foundation
struct Person {
var name: String
var age: Int
// var res = ""
// var res: String = ""
func walk(){
print("\(name) can walk")
}
func sleep(sleeptime: Int) {
print("\(name) can spleep \(sleeptime)")
}
func tech(a: String, b:String, c:Int) -> String {
return ("\(a) and \(b) can tech \(c) times")
}
func armleg(arms: Int, legs:Int) -> Int {
return arms + legs
}
}
输出:
Hello, World!
Tom
Tom can walk
Tom can spleep 36000
China and Japan can tech 900 times
40
22
30
3
Optional("litao")
Optional(30)
Program ended with exit code: 0