美文网首页
Swift学习1.0

Swift学习1.0

作者: Devbrave | 来源:发表于2016-10-06 16:36 被阅读48次
swift基本数据类型简介及基本使用
    var Xnum: Int = 15; //Int为指定数据类型
    var Xfloat: CGFloat = 14.1
    // 元组
    let student: (String, Int, Double, Int) = ("lisi",24,90.0,15)
    let student1 = (name:"zhangsan", age:26)//直接定义元组数据

    //可选值 optionals, 两种状态:有值,没有值(nil)
    var optValue: Int? = 3

    Xfloat = CGFloat(Xnum)//强转类型
        //元组数据提取
        print(student1.name)
        print(student1.age)
        let (name, age, score, number) = student
        print(name,age,score,number)
        
        if (optValue != nil){//可选值有值为真,无值为假
            print(optValue)
        }
        //表示对可选值的强制解析,可选值!(提取可选值)如可选值没有值时,不能进行强制解析。否则会有运行时的错误
        let svalue: Int = optValue!
        print(svalue)
        
        //可选绑定 (比强制解析更加安全)
        if let svalue1 = optValue {
            
            print(svalue1)
        }
        
        //隐式可选类型
        let intValue:Int = impOptValue
        print(intValue)
        
        //字符串操作
        let str1:String = "http:www.baidu.com"
        let str2:String = "1"
        let url:String = str1 + str2//字符串拼接
        print(url);
        
        let char = str1[str1.index(after: str1.startIndex)]//获取指定位置的字符
        print(char)
        
        let subString = str1[str1.startIndex...str1.index(str1.startIndex, offsetBy: 4)]
        print(subString)//截取指定位置的字符串
        
        let urlStr:String = "http:www.baidu.com/num=\(str2)"        
        let rangStr: Range = urlStr.range(of: str1)!
        print(rangStr)
        
//        let subUrlString = urlStr[urlStr.startIndex...urlStr]
        
        if urlStr.hasPrefix("http") {//判断字符串前缀
            print(urlStr)
        }
        
        if urlStr.hasPrefix(str2) {//后缀
            print(urlStr)
        }
        
        let studentValue:Student = self.getMultipleValues()
        print(studentValue)
        
        // 关系运算符
        let res:Bool = 5 == 5+3
        
        if res {
        }
        
        let testInt: Int = 3 > 2 ? 3 : 2//三元运算符
        print(testInt)
        
        for i in 1...5 {//for循环遍历
            print(i)
        }
        
        var array = [1,2,3,4];
        array.remove(at: 0);//删除数组中某个位置的元素
        var range1: Range<Int> = 1..<5
        var range2 = 1...6
        print(range2)
        print(range1)
        
        range1 = 0..<2
        range2 = 0...2
        array[range1] = [1,1]
        array.replaceSubrange(1..<2, with: [1,3])//替换某个区间的元素
        print("arry:=\(array)")
        
        for item in array {
            print("for in item: \(item)")
        }
        ///
        for (index, value) in array.enumerated() {//遍历数组 index为数组下表 value为对应的元素
            
            print("for enumerated index:\(index) value:\(value)")
        }

相关文章

网友评论

      本文标题:Swift学习1.0

      本文链接:https://www.haomeiwen.com/subject/qvalyttx.html