前端之家收集整理的这篇文章主要介绍了
Swift高级开发语言基础,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
var str = "Hello,playground"
var str1 = "abc"
str1 = "bcd"
var a = 10
var b = 20
let maxNumber = 57
var currentStudent: Int = 50
currentStudent++
var score = 90.2
var score1:Float = 90.5
var sumscore = score + Double(score1)
var sumscore1 = Float(score) + score1
let name = "赤发鬼"
let char:Character = "b"
let loClass = "150727"
let teaInfo = loClass + name
var strTest = "0123456789"
var index = strTest.startIndex
var index_end = strTest.endIndex
var index_p1 = index_end.predecessor()
var index_s1 = index.successor()
var subString = strTest.substringToIndex(index_s1)
var nsstring1 = (strTest as NSString).substringFromIndex(5)
var nsstring2 = (strTest as NSString).substringToIndex(4)
var strTest2 = "01234567"
let index_str2 = strTest2.startIndex.advancedBy(3)
var subStringTest2 = strTest2.substringToIndex(index_str2)
var cat = "��"
var �� = "生气"
var 太阳 = "��"
var 日天 = "��☁️"
let strCharacters:[Character] = ["a","b","c"]
let catStr = String(strCharacters)
print(catStr)
var array = [1,2,3]
var array2 = ["12","21","12"]
var array3 = [1,"2","3"]
var array4:[Int] = [1,3,4,5]
var array5:Array<String> = ["1","3"]
var array6:Array = [1,"8"]
array.append(4)
array.insert(2,atIndex: 1)
array.last
array.first
let c = array.removeAtIndex(1)
c
array[0] = 5
array
let d = array[1]
array += array4
array
array4
array[0...2]
array[0..<2]
array[0...2] = [11,22,33]
array[0..<2] = [44,55,66,777]
var dictionary = [1:"1",2:"2",3:"3"]
var dictionary1:[Int: String] = [1:"2"]
var dictionary2:Dictionary<String,Int> = ["1": 1]
let e = dictionary[1]
dictionary[1] = "a"
dictionary[4] = "4"
dictionary
var index_dic = dictionary.indexForKey(1)
dictionary.removeAtIndex(index_dic!)
var error:String? = nil
var score_xiaoming: Int? = nil
error = "error"
error! += "404"
error
var status = (404,"Not Found!")
status.0
status.1
var status2:(num:Int,info:String) = status
status2.num
let isOrNo = true
if isOrNo{
print("true")
} else {
print("false")
}
var count:Int? = nil
count = 20
if count > 0
{
print(count!)
print("数字count是\(count)")
} else {
print(count)
}
let str_pinjie = "数字count是\(count)"
if let count1 = count {
print("ok")
} else {
print("error")
}
var number = 0
while number < 3{
number++
print("number = \(number)")
}
var number1 = 2
repeat {
number1++
print(number1)
}
while number1 < 5
for (var number3 = 6; number3 < 10; number3++){
print("number3 = \(number3)")
}
for num in array {
print(num)
}
var dic = [33 : "value"]
for value in dic {
print(value)
value.0
value.1
}
for (key,value) in dic {
print(key)
print(value)
}
for (_,value) in dic {
print(value)
}
for (key,_) in dic {
print(key)
}
for value in 0 ..< 1 {
print(value)
}
for value in array[2..<3] {
print(value)
}
var point = (2,0)
switch point {
case (0,0):
print("原点")
case (_,0):
print("x轴")
fallthrough
case (0,_):
print("y轴")
default:
print("象限内")
}
switch point {
case(0,0):
print("原点")
case (let x,0):
print("x轴")
case (0,let y):
print("y轴")
default:
print("象限内")
}
switch point {
case(0,0)where x == 3 || x == 2:
print("x轴")
case (0,let y):
print("y轴")
default:
print("象限内")
}
var number2 = 20
switch number2 {
case 0...10:
print(number2)
case 11...100:
print(number2)
fallthrough
case 101...200:
print(number2)
default:
print(number2)
}
for value in 1...100 {
print(value)
if value == 2 {
break
}
}
var tempString = "kjahfksiiqsaqawgwqghf"
tempName: for tempStr in tempString.characters {
switch tempStr{
case "a","e","i","o","u":
print(tempStr)
break tempName
default:
print(tempStr)
}
}
原文链接:https://www.f2er.com/swift/325629.html