• swift(一)基础变量类型


    import Foundation
    
    println("Hello, World!")
    
    
    /*
        int a;
    */
    var a = 10 //隐式类型转换
    a = 9
    println(a)
    
    
    let b = 10
    println(b)
    
    
    /*
    类型标注
    */
    
    //var x:Int
    //x = 10.3不支持隐式转换
    
    let x: Int = 10
    
    /*
    名称不用在符合标示符的规范
    */
    let 常量 = 10
    
    /*
    基本数据类型:整数类型,小树类书
    Int8/16/32/64
    UInt8/16/32/64
    */
    let interger : Int = 10
    
    let unsignedInt:UInt32 = 19
    //let overValue:UInt8 = UInt8.max
    
    
    /*
    两种基本类型可不可以进行隐式转换
    */
    
    var intVar:Int = 10
    
    /*
    类型推断和类型安全
    */
    let stringValue = "hello"
    var uint8value : UInt8 = 9
    var uint16Value : UInt16 = 9
    var uint32Value :UInt32 = UInt32(uint8value)+UInt32(uint16Value)//类型转换
    
    
    /*
    类型转换,(类型)表达式
    */
    //let floatValue:Double = 1231323
    var floatValue:Double = 1231323
    intVar = Int(floatValue)
    println(intVar)
    
    floatValue = Double(intVar)
    
    /*
    布尔类型
    */
    let turnOffLight = 1
    if turnOffLight==1
    {
        println("Turn OFF")
    }
    else
    {
        println("Turn ON")
    }
    
    /*
    元组:(30,“hello”)
    */
    
    let student:(String,Int,Double) = ("zhangsan",23,98.5)
    println(student.0)
    println(student.1)
    println(student.2)
    
    let(name,age,_) = student
    println(name)
    println(age)
    //println(score)
    
    let student1 = (name:"zhangsan",age:23)
    println(student1.name)
    println(student1.age)
    
    /*
    可选值:optionals,两种状态:有值,没有值
    */
    
    var optValue: Int? = nil
    if (optValue != nil){
        println(optValue)
    }
    
    //表示对可选值的一个强制解析,可选值!
    //var svalue:Int = optValue!
    
    //可选绑定
    
    if var svalue = optValue{
        println(svalue)
    }
    
    /*
    隐式解析可选类型,两种状态,有值,没有纸nil
    */
    var imOptValue:Int! = 8
    if (imOptValue != nil){
        println(imOptValue)
    }
    if let ivalue = imOptValue
    {
        println(ivalue)
    }
    
    var ivalue:Int = imOptValue
  • 相关阅读:
    网易企业免费邮箱
    168. Excel Sheet Column Title
    167.Two Sum II-Input array is sorted
    166. Fraction to Recurring Decimal
    165 Compare Version Numbers
    164. Maximum Gap
    163.Missing Ranges
    162.Find Peak Element
    161.One Edit Distance
    160. Intersection of Two Linked Lists
  • 原文地址:https://www.cnblogs.com/keyan1102/p/4635251.html
Copyright © 2020-2023  润新知