Opaque Result Type: Some View 意思是返回的一定是一个确定胡,而且遵守view协议的类型.但具体类型不确定
/ 我的第一个 Swift 程序 /
var myString = "Hello world! - swift.jsrun.net" print(myString)
var list: [Int] = [3,4,5] print("(list[1])")
var listS = String listS.append("af") listS.append("ffff")
print(listS[1])
var shopList: [String] = ["d","y","z"] shopList[1] = "fd" print(shopList[1]) print("----------------") var person: [String:String] = ["name":"dyn","sex":"woman"] print(person["name"] ?? "hello") person["sex"] = "man" print(person["sex"] ?? "woman") print(person["dddd"] ?? "null")
print("-----------------")
let scores: [Int] = [33,55,77,44,88] var teamScore = 0 var lowScore = 0 for score in scores { if score > 50 { teamScore += 1
}
else {
lowScore += 1
}
} print(teamScore)
print("-----------------")
var optionStr: String? var green = "sadf" //optionStr 是否有值。“”也是值,只要经过赋值语句就是有值 // if 语句 是判断可选变量 if let name = optionStr { green += "," + name } else { green += ", null" } print(green)
//switch支持任意类型的数据及各种比较操作,无需break 但default必须有
let veatable = "red peper"
var vegatComment = ""
switch veatable {
case "adf":
vegatComment = "afadf"
case "red peper":
vegatComment = "ddd"
default:
vegatComment = "d"
}
print(vegatComment)
let numDic: [String:[Int]] = ["prive":[1,2,3], "prieeve":[12,22,32]] for(kind, numbers) in numDic { print("(kind)") for num in numbers{ print("(num)") } } print("--------------------------") var n = 2 while n < 100 { n = n * 2 print("(n)") }
var m = 2 repeat { n = m 2 }while m < 1 print("-----------") print("(n)") print("-----------") var loop = 10 for i in 0..<4 { loop = loop 3 print("(loop)") }
func greet(name: String, day: String) -> String { return "say hello to (name) at (day)" } print(greet(name:"dyn",day:"tUESDAY"))
func calculateStatic(scores: [Int]) -> (min: Int, max: Int, sum: Int) { var min = scores[0] var max = scores[0] var sum = 0 for item in scores { if item > max { max = item } else if item < min { min = item } sum += item
}
return (min, max, sum)
} let c = calculateStatic(scores: [5,3,2,10,16,51]) //c 为(min: Int, max: Int, sum: Int) 是元祖可以采用名称访问,也可以采用下标访问 print("(c.sum) (c.min) (c.1)")
print("---------------------")
func sumOf(numbers: Int...) -> Int { var tol = 0 for num in numbers { tol += num } return tol } let a = sumOf(numbers: 3,4,5,6,4) print("(a)") // 参数: 第一个名称是外部名称,第二个是内部名称,只有一个名称就是公用一个名称, //参数的默认修饰词是let。不可修改 ,外部名称可省略用代替 // 要修改参数的值需要用inout 修饰 .调用是传入的必须是变量的地址 &varb print("------------------------") func sumExchange1( a: inout Int, _ b: inout Int) -> Int { let temp = a a = b b = temp return a
} var c11 = 3 var c22 = 4989 let c2 = sumExchange1(&c11,&c22) print("(c2)")
func sumExchange(num1 a: Int, num2 b: Int) -> Int { let temp = a
return temp
} let c = sumExchange(num1:4, num2:10) print("(c)")
func sumOf1(_ numbers: Int...) -> Int { var tol = 0 for num in numbers { tol += num } return tol } let a1 = sumOf1( 3,4,5,6,4) print("(a1)")
//类
class NamedShape { var numberOfSides: Int = 0 var name: String = "" init(name: String) {
self.name = name
}
} class Square: NamedShape { var sideLength: Double init(sideLength: Double, name: String) { self.sideLength = sideLength super.init(name: name) numberOfSides = 4 } func area() -> Double { return sideLength * sideLength } } let test = Square(sideLength: 4, name: "a Square") let area1 = test.area() print("(area1)")
class Circle: NamedShape { var radius: Double init(radius: Double, name: String) { self.radius = radius super.init(name: name) numberOfSides = 0 } func area() -> Double { return 3.14 radius radius }
} print("afafdad") let circleTemp = Circle(radius: 10.4 , name: "circle") let area2 = circleTemp.area() print("(area2)") print("-------------------------")
// 结构体与类功能相同,唯一区别是结构体是传值,类是传引用 class EqualTraiangle: NamedShape { var sideLength: Double = 0.0 //子类初始化过程 1子类设置属性值 2.父类初始化 3.改变父类定义的属性值 init(sideLength: Double, name: String) { self.sideLength = sideLength super.init(name: name) numberOfSides = 3 } // 计算属性,就是写set get 方法的属性 var perimater: Double { get { return 3.0 * sideLength } set { sideLength = newValue / 3.0 } } } var traiangle = EqualTraiangle(sideLength: 12, name: "dd") print("(traiangle.perimater)")