//
// 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 = "" //初始化一个空的字符串 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 = (, "iphone 6s", )
print(product)
//获取元祖中的每一个元素
var (_year , _name , _price) = product print("year = \(_year)","_name=\(_name)", "price=\(_price)") //只输入一个值 let (_ ,name1, _) = product
print("name\(name1)") //可以使用点语法 let product2 = (year:, name:"iphone 6s", price:)
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 = ""
var value1 : Int? = Int(numStr)
print(value1) /**
数值得可读性
*/ let readabilty1 = ;
let readabilty2 = 1_000_000_000_1 /**
类型别名
*/ //类型别名就是给一个类型重新取一个更有意义的别名(新类型),它使用 typealias 关键字 //格式如下 typealias NewInt = Int32 var new_value : NewInt = print(new_value) /**
基本数据类型之间的转换
*/ // Int类型转换成其他的类型 var j :Int =
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 =
"\(i)" //不能直接用 String(Int) 可以将字符串的数值使用"\(value)"形式
//Int 可以直接使用 Double(Int) Float(Int) /**
运算符
*/ /**
++i 是 i= i+1 而--i 是 i = i-1 */
//一元运算符
let three = -
let minusThree = -three
let plusThree = -minusThree print(minusThree, plusThree) //as 类型转换
//is 类型检查 } override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} }

最新文章

  1. Makefile_:=与=的区别
  2. eval 与 Function
  3. DotNetBar v12.7.0.10 Fully Cracked
  4. Spring中PropertyPlaceholderConfigurer的使用
  5. 在wpf窗体上添加用户控件
  6. 更改 input type 的值
  7. HDU 4497 GCD and LCM (数学,质数分解)
  8. 格式太旧或是类型库无效。 (Exception from HRESULT: 0x80028019 (TYPE_E_UNSUPFORMAT))
  9. BZOJ 1507 Editor
  10. POJ 3292 Semi-prime H-numbers (素数筛法变形)
  11. 基本命令行操作1(java编译)
  12. 两种简单的方法Docker构建LANMP
  13. js通过a链接控制多个DIV只显示其中一个其它隐藏
  14. ng-model,ng-value,ng-bind,{{}}----angularJS数据绑定
  15. WebSocket(4)---实现定时推送比特币交易信息
  16. 2、JUC--CAS算法
  17. 使用v-for指令渲染列表
  18. OpenOCD Debug Adapter Configuration
  19. vb 读取指定路径文件名
  20. mysql 小数处理

热门文章

  1. Codevs No.2144 砝码称重2
  2. CodeForces 489D Unbearable Controversy of Being (搜索)
  3. JSF 2 listbox example
  4. thymeleaf学习
  5. Codeforces 622B The Time 【水题】
  6. URAL 2065 Different Sums (找规律)
  7. IP访问SQL数据库设置
  8. spring mvc 详细执行流程
  9. TypeScript学习笔记(二):基本数据类型及数据转换
  10. AOE关键路径