数据类型
This commit is contained in:
@@ -1,3 +0,0 @@
|
||||
import UIKit
|
||||
|
||||
var greeting = "Hello, playground"
|
@@ -1,4 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<playground version='5.0' target-platform='ios' buildActiveScheme='true' executeOnSourceChanges='false' importAppTypes='true'>
|
||||
<timeline fileName='timeline.xctimeline'/>
|
||||
</playground>
|
Binary file not shown.
@@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Timeline
|
||||
version = "3.0">
|
||||
<TimelineItems>
|
||||
</TimelineItems>
|
||||
</Timeline>
|
@@ -0,0 +1,25 @@
|
||||
import UIKit
|
||||
|
||||
// 注释的几种类型
|
||||
// 单行注释
|
||||
|
||||
/**
|
||||
多行注释
|
||||
*/
|
||||
|
||||
/**
|
||||
1.
|
||||
/*多行注释嵌套*/
|
||||
2.
|
||||
*/
|
||||
|
||||
// 开起markdown语法,在两斜杠之后加上冒号
|
||||
//: # 一级标题
|
||||
/*:
|
||||
# 学习Swift
|
||||
## 基础语法
|
||||
- 变量
|
||||
- 常量
|
||||
*/
|
||||
|
||||
//: [Next](@next)
|
@@ -0,0 +1,116 @@
|
||||
//: [Previous](@previous)
|
||||
|
||||
//: ### 常量
|
||||
// 只能赋值一次,用let声明,它不要求在编译期间确定
|
||||
// 常量以及变量在初始化之前都不能使用
|
||||
let age: Int
|
||||
age = 31
|
||||
|
||||
let name = "Zhang Mengxu"
|
||||
let sex: String = "man"
|
||||
|
||||
|
||||
//: ### 标识符
|
||||
// 标识符(比如常量名、变量名、函数名)几乎可以使用任何字符
|
||||
// 标识符不能以数字开头,不能包含空白字符、制表符、箭头等特殊字符
|
||||
|
||||
//: ### 常见的数据类型
|
||||
/*:
|
||||
常见的数据类型:
|
||||
- 值类型(value type)
|
||||
- 引用类型(reference type)
|
||||
|
||||
值类型(value type)
|
||||
值类型包含:
|
||||
- 枚举(enum):
|
||||
Optional
|
||||
- 结构体(struct):
|
||||
Bool、Int、Float、Double、Character、String、Array、Dictionary、Set
|
||||
|
||||
引用类型(reference type)
|
||||
引用类型包含:
|
||||
- 类(class)
|
||||
*/
|
||||
|
||||
//: #### 整数类型
|
||||
/*:
|
||||
整数类型:Int8、 Int16、 Int32、 Int64、 UInt8、 UInt16、 UInt32、 UInt64
|
||||
在32bit平台,Int等价于Int32, Int等价于Int64
|
||||
整数的最值:UInt8.max、 Int16.min
|
||||
一般情况下,都是直接使用Int即可
|
||||
*/
|
||||
let a:Int = 10
|
||||
print(Int.max)
|
||||
|
||||
|
||||
//: #### 浮点类型
|
||||
/*:
|
||||
浮点类型:Float,32位,精度只有6位Double,64位,精度至少15位
|
||||
*/
|
||||
let letFloat:Float = 30.0
|
||||
let letDouble:Double = 30.0
|
||||
|
||||
//: #### 字面量
|
||||
// 布尔值
|
||||
let bool = true
|
||||
|
||||
// 字符串
|
||||
let string = "Zhang Mengxu"
|
||||
|
||||
// 字符
|
||||
let character: Character = "🐶"
|
||||
|
||||
// 整数
|
||||
let intDecimal = 17 // 十进制
|
||||
let intBinary = 0b1001 // 二进制
|
||||
let intOctal = 0o21 // 八进制
|
||||
let intHexadecimal = 0x11 // 十六进制
|
||||
|
||||
// 浮点数
|
||||
let doubleDecimal = 125.0 // 十进制,等价于1.25e2、 0.0125、 1.25e-2
|
||||
let doubleHexadecimal1 = 0xFp2 //十六进制,意味着15x2^2,相当于十进制的60.0
|
||||
let doubleHexadecimal2 = 0xFp-2 // 十六进制,意味着15x2^-2,相当于十进制的3.75
|
||||
|
||||
// 数组
|
||||
let array = [1, 3, 5, 7]
|
||||
|
||||
// 字典
|
||||
let dictionary = ["name": 34, "age": 18]
|
||||
|
||||
//: #### 类型转换
|
||||
// 整数转换
|
||||
let int1: UInt16 = 2_000
|
||||
let int2: UInt8 = 1
|
||||
let int3 = int1 + UInt16(int2)
|
||||
|
||||
// 整数浮点数转换
|
||||
let int = 3
|
||||
let double = 0.14159
|
||||
let pi = Double(int) + double
|
||||
let intpi = Int(pi)
|
||||
|
||||
//: #### 元组(tuple)
|
||||
// 元组的定义
|
||||
let httpError = (404, "Not Found")
|
||||
|
||||
// 元组访问
|
||||
httpError.0
|
||||
httpError.1
|
||||
print("The status code is \(httpError.0)")
|
||||
|
||||
// 元组的赋值
|
||||
let (statusCode, statusMessage) = httpError
|
||||
print("The status message is \(httpError.1)")
|
||||
|
||||
// -代表的是不接收参数值,Not Found会忽略
|
||||
let (justTheStatusCode, _) = httpError
|
||||
|
||||
// 元组中添加参数,使用的时候通过名称访问,而非数字
|
||||
let http200Status = (statusCode: 200, description: "ok")
|
||||
print("The status code is \(http200Status.statusCode)")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//: [Next](@next)
|
7
SwiftStudy.playground/contents.xcplayground
Normal file
7
SwiftStudy.playground/contents.xcplayground
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<playground version='6.0' target-platform='ios' display-mode='raw' buildActiveScheme='true' importAppTypes='true'>
|
||||
<pages>
|
||||
<page name='01 注释'/>
|
||||
<page name='02 数据类型'/>
|
||||
</pages>
|
||||
</playground>
|
7
SwiftStudy.playground/playground.xcworkspace/contents.xcworkspacedata
generated
Normal file
7
SwiftStudy.playground/playground.xcworkspace/contents.xcworkspacedata
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Workspace
|
||||
version = "1.0">
|
||||
<FileRef
|
||||
location = "group:">
|
||||
</FileRef>
|
||||
</Workspace>
|
Binary file not shown.
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>SchemeUserState</key>
|
||||
<dict>
|
||||
<key>SwiftStudy (Playground).xcscheme</key>
|
||||
<dict>
|
||||
<key>isShown</key>
|
||||
<false/>
|
||||
<key>orderHint</key>
|
||||
<integer>0</integer>
|
||||
</dict>
|
||||
</dict>
|
||||
</dict>
|
||||
</plist>
|
Reference in New Issue
Block a user