swift基本数据类型的使用

//
//  ViewController.swift
//  基本数据类型
//
//  Created by 叶炯 on 16/9/8.
//  Copyright © 2016年 叶炯. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

//        let //常量
//        var //变量
//
        //使用let 必须初始化 let 的值不可改变

        /*
        字符,字符串
        */
        //字符
        var charValuel : Character = "q"
        //字符串
        var charValue2 : String = "123"

        //初始化一个空的字符串

        var charValue3 = ""
        var charValue4 = String()

        //判断一个字符串是否为空
        if charValue2.isEmpty {
            print("字符串为空")

        }

        //或

        if charValue3 == "" {
            print("字符串为空")
        }

        //给字符串赋值
        let emptyString = "abc"
        let emptyString2 = String("abc")

        //遍历一个字符串中的字符
        for c in emptyString2.characters {

            print(c)
        }

        /**
        元祖
        */

        //元祖内的值可以是任意类型,各元祖不必是相同的类型

        //定义一个元祖

        let  product = (2015, "iphone 6s", 6088)
        print(product)
        //获取元祖中的每一个元素
        var (_year , _name , _price) = product

        print("year = \(_year)","_name=\(_name)", "price=\(_price)")

        //只输入一个值

        let (_ ,name1, _) = product
        print("name\(name1)")

        //可以使用点语法

        let product2 = (year:2015, name:"iphone 6s", price:6088)
        print(product2.year)
        print(product2.name)
        print(product2.price)

        /*
        可选类型
        */

        var stringValue : String
       // print(stringValue) 编译不会通过, 没有初始化

        var stringValue2 : String?
        if (stringValue2 != nil) {

            print(stringValue2!)

        }
        //如果字符串为""使用!打印会崩溃 使用? 输出为nil
        //!表示一定有值 ? 表示可能有值
        var numStr : String = "123"
        var value1 : Int? = Int(numStr)
        print(value1)

        /**
        数值得可读性
        */

        let readabilty1 = 100000000;
        let readabilty2 = 1_000_000_000_1

        /**
         类型别名
        */

        //类型别名就是给一个类型重新取一个更有意义的别名(新类型),它使用 typealias 关键字

        //格式如下

        typealias NewInt = Int32

        var new_value : NewInt = 123

        print(new_value)

        /**
        基本数据类型之间的转换
        */

        // Int类型转换成其他的类型

        var j :Int = 3
        Double(j)
        Float(j)

        //String 类型转 Int Float Double 类型

        var s : String = "2.156asd2"

        //当第一个字符不是数字时,转为 Double 就为0
        //当字符第一位为数字,可以直接转为数字,,遇到非字符时停止,数值为非字符之前的数字
        Int(s)
        print("-------",s)

        var s2 = s as NSString

        print(s2.integerValue)
        print(s2.floatValue)
        print(s2.doubleValue)

        //Double Float Int 转 String

        var d : Double = 1.68
        "\(d)"

        var f : Float = 3.134
        "\(f)"

        var i :Int = 1
        "\(i)"

        //不能直接用 String(Int) 可以将字符串的数值使用"\(value)"形式
        //Int 可以直接使用 Double(Int) Float(Int)

        /**
        运算符
        */

        /**
        ++i 是 i= i+1   而--i 是 i = i-1

        */
        //一元运算符
        let three = -3
        let minusThree = -three
        let plusThree = -minusThree

        print(minusThree, plusThree)

        //as 类型转换
        //is 类型检查

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
时间: 2024-08-09 06:21:05

swift基本数据类型的使用的相关文章

Swift基础数据类型和循环

本文是一篇swift的基础教材,讲解了swift语言的变量,数据类型和循环.准备学swift的同学可以参考学习下. 1 常量变量 var myVariable = 42 //变量       myVariable = 50       let myConstant = 42 //常量 2 显示指明变量类型 let explicitDouble: Double = 70 3 数组和字典 var shoppingList = ["catfish", "water", &

Swift基本数据类型_01_Var_let

//: Playground - noun: a place where people can play //简洁的方式来包含框架,直接写框架名称 import UIKit //var str = "Hello, playground" //println(); //swift1.0中使用此函数来打印 print("hello world") //swift2.0打印方式 //1.与OC的区别 /* /* (1)语句后面不需要加: (2)注释可以嵌套,即多行注释中可

Swift基础数据类型

常量和变量 常量和变量由一个特定名称来表示,如maximumNumberOfLoginAttempt 或者 welcomeMessage.常量所指向的是一个特定类型的值,如数字10或者字符”hello”.变量的值可以根据需要不断修改,而常量的值是不能够被二次修改的. 常量和变量的声明 常量和变量在使用前都需要声明,在Swift中使用let关键词来声明一个常量,var关键词声明一个变量.如下面例子 let maximumNumberOfLoginAttempts = 10 var currentL

Swift学习—数据类型

知识点: 指定常量\变量的数据类型 Swift严格要求变量在使用之前必须进行初始化 最值:可以使用min和max方法获得最值 范围:数值超出存储范围Swift会报错 类型不同的两个变量不能相加 数字格式:可以增加零(0)或者下划线(_)增强可读性 类型转换 Swift中使用typealias关键字定义类型的别名,跟C中的typedef作用类似 示例代码: //: Playground - noun: a place where people can play import UIKit // Sw

swift基本数据类型

1 // Playground - noun: a place where people can play 2 3 import UIKit 4 //----------------------------------------------------------------------- 5 //1.字符串输出 6 var str = "world" 7 println("hello, \(str)") 8 9 //-----------------------

swift -基本数据类型的定义与使用

// // main.swift // swift-02 // import Foundation //Foundation 基础库包 //没有main函数 //这个函数的作用就是打印信息 //这里可以不用加; //lua语言 // println("Hello, World!") //变量定义 定义了一个变量 //int a;变量定义以var开头,var开头就是声明一个变量,变量名需要写初始值 //变量名还可以再次赋值 //\()表示可以对()的内容做计算 var myVar = 0

swift 基本数据类型定义

infix operator & { associativity left precedence 150 } infix operator !== { associativity none precedence 130 } infix operator >= { associativity none precedence 130 } infix operator ~= { associativity none precedence 130 } infix operator < { as

Swift的数据类型

引用类型就是在赋值或给函数传递参数对的时候,把本身数据传递过去,这样在函数的调用过程中,会影响原始数据,类属于引用类型 1.整型: Int8:有符号8位整型: Int16:有符号16位整型 Int32: Int64: Int:平台相关有符号整型 UInt8:无符号8位整型 UInt16:无符号16位整型 UInt32: UInt64: UInt:平台相关无符号整型 2.浮点型 float: Double: 数字的表示方式: 为一个整数变量赋值十进制,二进制,十六进制,他们的表示方式如下, (1)

SWIFT——元组数据类型(tuples)

文章为自己学习所用,系转载.为学习51cto课程的笔记.如有侵权,请私信本人进行删除. 链接如下. ?http://edu.51cto.com/roadmap/view/id-58.html 1.元组常量/变量的定义 2.从元组数据中提取每一个元互不的值 3.为元组数据中的每一个元素命名 1 let product1 = (20, "iPhone6", 5888); 2 let product_name = "iPhone7" 3 var product2 = (3