现在的位置: 首页 > 综合 > 正文

swift语法

2018年03月20日 ⁄ 综合 ⁄ 共 1477字 ⁄ 字号 评论关闭

    println("Hello World")
    
    var myVariable = 42
    myVariable = 50
    let myConstant = 42

    let implicitInteger = 70
    let implicitDouble = 20.0
    let explicitDouble:Float = 70

    let myVariableInteger = 4
    let myVariableFloat = 2.0

let label = "the width is "
let width = 100
let widthLabel = label + String(width)


let apples = 3
let oranges = 5
let appleSummary = "I have \(apples) apples."
let fruitSummary = "I have \(apples + oranges) pieces of fruit."

//数组和字典
var shoppingList = ["apple","water","tulips","blue paint"]
shoppingList[1]

var occupations = [

    "Malcolm":"Captain",
    "kaylee":"Mechanic",

]
occupations["Jayne"] = "Public Relations"

println(occupations["Jayne"])
println(occupations["Malcolm"])

//要创建一个空数组或者字典,使用初始化语法。
let emptyArray = [String]()
let emptyDictionary = Dictionary<String,Float>()


//控制流
let individualScores = [10,20,30,40,50]
var teamScore = 0
for score in individualScores{
    if score > 20{
        teamScore += score
    }
    println(score)
}
teamScore
    
var optionalString:String? = "Hello"
optionalString == nil
//var optionalName:String? = "Cheng Liang"
var optionalName:String? = nil
var greeting = "Hello!"
if let name = optionalName{
    greeting = "Hello,\(name)"
}else{
    greeting = "Hello,World"
}

//switch
let vegetable = "abc pepper"
switch vegetable{
case "celery":
    let vegetableComment = "Add some raisins and make ants on a log."
case "cucumber","watercress":
    let vegetableComment = "That would make a good tea sandwich."
case let x where x.hasSuffix("pepper"):
    let vegetableComment = "Is is a spicy \(x)"
default:
    let vegetableComment = "Everything tastes good in soup"
}

var n = 1,m = 0
while n <= 100{
    m += n
    n++
}
m
    
var k = 0,j = 1
do{
    k += j
    j++
}while j <= 100
k
    
var firstForLoop = 0
for z in 1...100{
    firstForLoop += z

}
firstForLoop

抱歉!评论已关闭.