Swift基础语法的学习

Swift基础语法的学习

1.Swift中的模块化的标注;

在OC中使用是:

#pragma mark - 标注一段的相关功能;

在Swift中使用的是:

MARK:- 标注一段的相关功能;

实现的效果如下图:


2.Swift的基本“输入”,“输出”语句,常量,字符串,数学运算符,布尔类型等等

//MARK:- 隐式声明类型
        //MARK:- 输出语句
        //swift的输出语句
        print("MeiNv");
        print("!");  //print此语句自带换行符
        

        //MARK:- 定义变量
        //定义变量给变量赋值(swift会自动推断变量的类型)
        var  x = 12
        print(x);
        
        var y = 42.0
        print(y);
        
        
        x = 38;
        y = 99;
        
        print(x);
        print(y);
        
        //变量的结果赋值给变量(如果两个变量的类型不相同应该强转一下,否则会报错)
        y = Double(x);
        print(y);
        
        //MARK:- 常量
       //常量使用let修饰
        let z = x;
        print(z);
        
        
        //z = 4;  //常量是不能改变的量,如果给常量赋值会造成错误
        
        //查看数值类型的上下线
        print(Int.min);
        print(Int.max);
        
        
        //数值转字符串
        var t  = 123;
        var s = String(t);
        print(s);
        
        //字符串转数值
        var u = Int(s);
        print(u);
        
        
        //MARK:- 显示声明类型
        var myNewNumber : Double = 3;
        print(myNewNumber);
        
        
        let myState = "i love";
        let myBobdy : String = "yuanyuan";
        
        let scentence = myState + " " +  myBobdy + ".";
        print(scentence);
        
        
        let myStr : Character = "A";
        
        let myFavoriteLetters = String(myStr)  + String(myStr);
        print(myFavoriteLetters);
        
        
        //MARK:- 数学运算符
        let addition = 2 + 2;
        print(addition);
        
        let subtraction = 4 - 3;
        print(subtraction);
        
        
        let multiplication = 10 * 5;
        print(multiplication);
        
        
        let division = 24/6;
        print(division);
        
        let modulo = 23 % 4;
        print(modulo);
        
        //swift中可以将求余运算用于Double值
        let modulo1 = 23 % 4;
        print(modulo);
        
        //数值前边加正号或者负号,即表示正数或者负数
        var positiveNumber : Int = +33;
        
        var positiveNumber1 : Int = -55;
        print(positiveNumber);
        print(positiveNumber1);
        
        let r = 3  + 5*9;
        print(r);
        
        let r1 = 48 / 5.0; //此时r1被指定为Double类型
        print(r1);
        
        
        //MARK:- 数值表示
        //二进制(0b)、八进制(0o)、十六进制(0x)
        let binaryNumber = 0b110011;
        print(binaryNumber);
        
        let octalNumber = 0o12;
        print(octalNumber);
        
        let hexadecimalNumber = 0x32;
        print(hexadecimalNumber);
        

        //Bool类型结果:true 和 false
        print(100 > 50);
        print(66.2 < 20);
        
        let result : Bool = "this thing" == "this hg";
        print(result);
        
        
        //嵌入表示法\()
        let myFavoriteStr = "new land";
        let myFood = "i love";
        print("when i \(myFavoriteLetters)\(myFood) years ago");
        
        
        //重定义别名
        typealias eightBits = UInt8;
        var reg : eightBits = 0;
        
        //元组:一组组合方式
        let myDreamCar = (2014,"mercedes-Ben","yuanyuan");
        print(myDreamCar);
        print(myDreamCar.0); //访问元组中的元素
        
    

        //可选类型(在相关类型后边加?即可)
        var v : Int? = nil;
        
        //MARK:- 使用集合
        let candyJar = ["Apple","goole bears","happy"];
        print(candyJar);


基本语法的学习就到这里了,稍后继续。。。。


你可能感兴趣的:(Swift基础语法的学习)