swift-01

var red,green,blue:Double //var 声明变量
red = 0.5
green = 30.5
blue = 234
let age = 18 //let 声明常量
/***
swift 使用(常量或者变量)把常量或者变量打印出来
swift用字符串插值的方式把常量名或者变量名当做占位符就到长字符串中,swift会用当前变量或常量的值替换这些占位符。降变量或常量放入圆括号中,并在开括号钱使用反斜杠将其转义:
/
print("red===(red)\ngreen===(green)\nblue===(blue)\nagr==(age)")
/
第一个多行注释的开头/第二个被嵌套的多行注释 / 第一个多行注释的结尾 /
let name = "陈大爷";print("我是(name)") // 这里的;必须使用
/*************************类型别名****************************************/
typealias AudioSample = UInt16
var maxAmplitudeFound = AudioSample.max; print("(maxAmplitudeFound)")
/************************布尔值*********************************************/
let orangesAreOrange = true
let turnipsAreDelicious = false
let i = 1
if i==1 {
}else {}
/*************************元组*************************************/
/

元组把多个值组合成一个复合值。元组内的值可以是任意类型,并不要求是相同类型
*/
let http404Error = (404,"Not Found")
let (statusCode,stausMessage) = http404Error
print("error is (statusCode)")
let (justStatusCode,_) = http404Error
print("errorCode is (justStatusCode)")
print("errorCode is (http404Error.1)")

    let http200Status = (statusCode:200,description :"ok")
    print("the status code is\(http200Status.statusCode)")

/***********************************************************************/ let possibleNumber = "123"
let convertedNumber = Int(possibleNumber)
let serverResponseCode: Int? = 404
print(serverResponseCode ?? 404)
// 需要一个跨越多行的字符串 “”“ ”“” 需要使用 两队(3个双引号)
let morelineString = """
徐璈\n
偶是很大很百搭大家看看打开的的\n
shdhadlkjkajdja睡觉觉啊
"""
print(morelineString)

    var emptyString = ""            //空字符串字面量
    var abotherEmptyString = String() //初始化方法
    //判断字符串是否为空  isEmpty
    if emptyString.isEmpty{
        print("nothing to see here")
    }
    //字符串可变性
    var variableString = "Horse"
    variableString += "and carriage"
    print(variableString)
    
    let constantString = "Highlander"

// constantString +="and carriage another" //常量不可修改,变量可以修改
for character in "老虎不发威你当我是" { //for in 循环遍历
print(character)
}
//Character 字符串可以通过传递一个值类型为Character的数组作为自变量来初始化:
let catCharacters : [Character] = ["c","a","t","!",""]
let catString = String(catCharacters)
print(catString)

    //连接字符串和字符
    let string1 = "hello"
    let string2 = "world"
    var welcome = string1 + string2
    print(welcome)
    var instruction = "look over"
    instruction += string2
    print(instruction)
    
    let exclamationMark : Character = "!"
    welcome.append(exclamationMark)
    print(welcome)
    let regionalIndicatorForUS: Character = "\u{1F1FA}\u{1F1F8}"
    print(regionalIndicatorForUS)
    //根据下标得到字母
    let greenting = "guten  tag"
    greenting[greenting.startIndex]
    greenting[greenting.index(before: greenting.endIndex)]
    greenting[greenting.index(after: greenting.startIndex)]
    let index = greenting.index(greenting.startIndex, offsetBy: 7)
    greenting[index]
    for index in greenting.indices {
        print("\(greenting[index]) ", terminator: "")
    }![屏幕快照 2018-03-13 上午9.22.21.png](https://upload-images.jianshu.io/upload_images/5998477-5ef473c3159eb6d7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

你可能感兴趣的:(swift-01)