码迷,mamicode.com
首页 > 编程语言 > 详细

Swift初体验 (一)

时间:2016-01-23 12:52:38      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

//  声明一个常量
let maxNumberOfStudents: Int = 47

//  声明一个变量,假设没有在声明的时候初始化,须要显示的标注其类型
var currentNumberOfStudents = 23

//  使用前必须初始化
currentNumberOfStudents += 1

//  浮点数据自己主动推断为Double类型,假设须要指定了Float类型,须要显示的指定Float类型
let score: Float = 90.5;

let englishScore = 80
let chineseScore = 90.5;

//  不同类型的数据不能直接进行运算,须要自己手动转换为同样的格式,格式是:Type(Value)
let allScore = Double(englishScore) + chineseScore

println("Int8 max value = \(Int8.max)")

var name: String = “HMT"

//  显示的声明一个字符类型
let sex: Character = “m"



var names: Array<String> = ["hmt","wdq1"]
var namesT: String[] = ["hmt","wdq"]
//var nameQ: NSArray = ["aaa","bbb","ccc"]
namesT += "qqqqq”
//  数组即使是不可变的,依旧能够改变它的值
namesT[1] = “1111"
//  2个点半开半闭  3个点全闭
namesT[1..2] = ["buzhidao","heheheheh"];
println("\(namesT)”)



for (index,value) in enumerate(namesT){

    println("\(index) \(value)")
}

var sexDic: Dictionary<String,String> = ["zhuwenpeng":"m","zhaoweisong":"m”]
//  假设字典中不存在这个键值对,则会加入;假设存在,就会替换改动
sexDic["chenfengchang"] = "m"
println(sexDic["chenfengchang"])
for (key,value) in sexDic{

    println("key = \(key) value = \(value)")
    println(sexDic["chenfengchang"])
}
//  "_"代表忽略元祖中的某个位置
for (_,value) in sexDic{

    println("value = \(value)")
}

let firstSex = sexDic["chenfengchang”]
//  字符串推断用”=="
let isMale = firstSex == "m"
//  推断仅仅接受布尔值
if isMale{

}else{

}

//  元组
let status:(Int, String) = (404,"Not Found")
println("statusCode = \(status.0) statusMessage = \(status.1)")

let status1:(statusCode:Int,statusMessage:String) = (statusCode:404,statusMessage:"Not Found")
println("statusCode = \(status1.statusCode) statusMessage = \(status1.statusMessage)")

let (statusCode,statusMessage) = (404,"Not Found")
println("statusCode = \(statusCode) statusMessage = \(statusMessage)")


let countStr = "333"
// toInt()方法返回的是一个Int类型的可选变量,可能为空,加”?"修饰
let count: Int? = countStr.toInt()

if count{
    let realCount = count!  //  强制解析
    println("realCount = \(realCount)")
}else{
    
}
//  先推断count是否有值,假设有值则把值付给realCount,假设没有就进入else
if let realCount = count{
    println("realCount = \(realCount)")
}else{

}

let point = (x: 2,y: 2)
switch(point){
case (0,0):
    println("")
case (let x,0):
    println("")
case (0,let y):
    println("")
case (let x,let y):
    println("")
}

switch(point){
case (let x,let y) where x == y:
    println("point在 x=y 这条线上")
case (let x,let y) where x == -y:
    println("")
case (let x,let y):
    println("")
}

let number = 122
switch(number){
case 0...9:
    println()
case 10...99:
    println()
case 100...999:
    println()
    fallthrough  //  相当于C语言中的switch不加break效果
default:
    println()
}

let tempString = "iyoipolk"
tempName: for letter in tempString{
    switch(letter){
    case "a", "e", "i", "o", "u":
        println("letter 是一个元音字母")
        break tempName
    default:
        println("letter 是一个辅音字母")
    }
}

Swift初体验 (一)

标签:

原文地址:http://www.cnblogs.com/lcchuguo/p/5153019.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!