前端之家收集整理的这篇文章主要介绍了
swift (二),
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
var number1 = 10.0
var number2 = -number1
println((number2))
var number3 = +number2
println((number3))
var number4 = number1++
number4++
println((number4))
number4--
println((number4))
let a:Double = 1.0
var quyuNumber = 10.0 % 3.0
println((quyuNumber))
var zuheNumber = 10
zuheNumber += 10
println((zuheNumber))
let aa = 1
let bb = 5
for qujianNumber in aa...bb {
println((qujianNumber))
}
15..<25
for qujianNumber2 in 15 ..< 25 {
println((qujianNumber2))
}
var optionalNumber:Int! = 10
println(optionalNumber!)
let kongheNumber = optionalNumber ?? 20
println((kongheNumber))
var string1 = "cacavca"
var string2 = "wang" + "zhen" + "gang"
println((string1))
println((string2))
var string3 = "\"wang\""
println(string3)
var string4 = "\'zhengang\'"
println(string4)
var appendingStr1 = "wangzhigang"
var appendingStr2 = appendingStr1
appendingStr2 += "shuai"
println((appendingStr1))
println((appendingStr2))
var initString:String = String(appendingStr1 + "age")
println((initString))
initString += "hgkehg"
println((initString))
var changeString1 = "good boys"
var changeString2 = changeString1.uppercaseString
println((changeString2))
println((changeString2.lowercaseString))
println((changeString2.capitalizedString))
var insertNumber = 18
println("insertNumber = \(insertNumber)")
var array = ["wang","zhen", 9]
println((array))
array.append("wang")
println((array))
array += ["wzg1","wzg2"]
println((array))
array.insert("wzg3",atIndex: 0)
println((array))
array.removeAtIndex(0)
println(array)
array[0] = "youmo"
println(array)
array[0...3] = ["1","2","3"]
println(array)
for value in array {
println("value = \(value)")
}
for (index,value) in enumerate(array) {
println("\(index):\(value)")
}
var initArray1:[String] = [String]()
var initArray2:[String] = [String](count: 3,repeatedValue: "wang")
println(initArray2)
var initArray3:Array<String> = Array(count: 4,repeatedValue: "zhen")
println((initArray3))
var dictionary = ["boy":"wang","age":"12","gender":"man"]
println(dictionary)
println(dictionary["age"])
dictionary["girl"] = "shiyingying"
println((dictionary))
dictionary.removeValueForKey("age")
println("delete : \(dictionary)")
dictionary["girl"] = "liujingran"
println((dictionary))
let returnValue = dictionary.updateValue("18",forKey: "age")
println("returnValue = \(returnValue),dictionary = \(dictionary)")
let returnValue2 = dictionary.updateValue("19",forKey: "class")
println("returnValue2 = \(returnValue2),dictionary = \(dictionary)")
for (key,value) in dictionary {
println("key = \(key) value = \(value)")
}
var initDiction1 : [String : String] = [String : String]()
var initDiction2 : Dictionary<String,String> = Dictionary()
for value in 1...10 {
println(value)
}
for _ in 1...10 {
println("wangzhengang")
}
for var index = 0 ; index < 10 ; index++ {
println("index = \(index)")
}
var whileNumber = 0
while whileNumber < 10 {
println("whileNumber = \(whileNumber)")
whileNumber++;
}
do {
println("111112")
}while(false)
var houValue = 20
if houValue <= 21 {
println("小于等于21")
}
else {
println("大于21")
}
var switchNumber = 12
switch switchNumber {
case 2, 3, 6, 10:
break
case 11:
println("11")
case 12, 123:
println("I am 12")
fallthrough
case 13:
println("I am 13")
default :
println("qita")
}
var (x,y) = (10, 0)
switch (x,y) {
case (let xValue, 0):
println(xValue)
case (0,let yValue):
println(yValue)
default:
println("others")
}
var domianNumber = 50
switch domianNumber {
case 0...10:
println("几块钱,穷逼")
case 11...100:
println("几十块,也挺穷")
default :
println("土豪!")
}
wang: while true {
for var labelNumber = 0; labelNumber < 100 ; labelNumber++ {
if labelNumber == 5 {
break wang
}
println("labelNumber = \(labelNumber)")
}
}
原文链接:https://www.f2er.com/swift/326619.html