Swift_Lesson1 基础数据类型 元组 基本用法

//

//  ViewController.swift

//  SwiftLesson_1

//

//  Created by 薛雨仑 on 14-10-2.

//  Copyright (c) 2014年 Dylan. All rights reserved.

//


import UIKit


class ViewController: UIViewController {


    overridefunc viewDidLoad() {

        super.viewDidLoad()

        

        // newButton

        var button = UIButton();

        button.frame = CGRectMake(20, 20, 280, 20);

        button.backgroundColor = UIColor.blueColor();

        self.view .addSubview(button);

        

        // 变量

        var string = "Hello"

        var number = 10

        

        // 常量

        let constString = "constString"

        let constInt = 20

        

        println(string)

        println(number)

        println(constString)

        println(constInt)

        

        // string

        var str_1 = "Alice"

        var str_2 = "Dylan"

        var str = str_1 + " and " + str_2

        println(str)

        

        // int string  \(Var)

        let num = 24

        var name = "Alice"

        println("she's name is \(name), age is \(num)")

        

        // kind type can't add

        let num_1 = 20;

        var name_1 = "Dylan"

        var des = "my name is \(name_1)" + ", age " + String(num_1)

        println(des)

        

        /**

        Int、Float、Double、Bool、Character、String

        

        Array、Dictionary、元组类型(Tuple)、可选类型(Optional)

        

        注意:数据类型的首字母都是大写的

        */

        

        // 指定变量的类型一般来讲,不用指定。 swift自动判断是什么类型

        let age:Int = 10

        

        // 变量在使用之前必须初始化

        let a = 10;

        

        // 最值输出一种数据类型的范围

        let minValue = UInt16.min

        let maxValue = UInt16.max

        

        println("\(minValue), \(maxValue)")

        

        // 尽量使用int

        /**

        (1)十进制数:没有前缀

        

          let i1 = 10 // 10

        

        (2)二进制数:以0b为前缀

        

          let i2 = 0b1010 // 10

        

        (3)八进制数:以0o为前缀

        

          let i3 = 0o12 // 10

        

        (4)十六进制数:以0x为前缀

        

          let i4 = 0xA // 10

        */

        

        // 如果不明确说明浮点数默认为double类型

        let number1 = 1.213

        

        let number2 = 12e2

        

        println(number2)

        

        /**

        数字可以增加额外的格式,使它们更容易阅读

        

        (1)可以增加额外的零 0

        

        let money = 001999 // 1999

        

        let money2 = 001999.000 // 1999.0

        

        (2)可以增加额外的下划线 _ ,以增强可读性

        

        let oneMillion1 = 1_000_000 // 1000000

        

        let oneMillion2 = 100_0000 // 1000000

        

        let overOneMillion = 1_000_000.000_001 // 1000000.000001

        

        说明:增加了额外的零  0和下划线 _ ,并不会影响原来的数值大小

        */

        

        let money = 1_000_000_000_000

        println(money)

        

        // 两个类型不同的数值不能直接运算需要强制转换

        let money_1:Int = 20

        let money_2:Int16 = 1

        let monet_3 = money_1 + Int(money_2)

        

        // 可以在初始化的时候混合相加

        let number_dou = 3 + 0.14

        

        // typealias 重命名相当于oc typedef

        typealias MyInt = Int

        let number_my:MyInt = 10

        

        // 1对1赋值多对多赋值

        let v = 10

        var c = 20

        

        let (x, y, z) = (1, 2, 3)

        

        // 跟C\OC不一样的是:Swift的赋值运算符没有返回值

        // 一定要记住没有返回值不可以去做判断条件的哦

        

        // swift 的取余支持浮点数

        let douNum = 1.41

        println(douNum%1.1)

        

        // 注意:在C语言中:0是假,非0就是真;而在Swift中没有这种概念

        // 所以在swift中的判断只能用true 或者 false 来判断 

        

        /**

        *  @Author Hacker-David.Xue, 14-10-02 16:10:56

        *

        *  闭合范围运算符:a...b,表示[a, b],包含a和b

        

           半闭合范围运算符:a..<b,表示[a, b),包含a,不包含b

        */

        for i in0...5 {

            println(i)

        }

        

        for i1 in0..<5 {

            println(i1)

        }

        

        // 元组 let 创建的是不可变的

        let positon = (x: 10, y:20)

        let person = (name: "Alice")

        

        var(x1, y1) = (10, 20)

        var position_1 = (10, 20)

        println(position_1.0)

        println(position_1.1)

        

        var position = (x: 10, y: 20)

        println(position)

        

        // 指定元素的类型就不能指定元素的名称

        var position_3: (Int, String) = (10, "string")

        

        // 赋值

        var position_4 = (20, 30)

        var (x3, y3) = position_4

    }

    


    overridefunc didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

}


 @ 

评论

© 小小技术博客 | Powered by LOFTER