import Foundation
/*
1.常量与变量
note:常量的值一旦设定就不能改变,而变量的值可以随意更改。
*/
//-----声明常量和变量
let v1 = 10;
//v1 = 9; 错误,常量值不可更改,编译器报错。
var v2 = 1;
v2 = 2;//正确
/*
2.类型标注
note:
1.如果没有指定表达式的类型, 也没有赋予初始值, 编译器会报错
2.指定类型的常量或者变量,不能赋予其他类型的值
3.不指定类型,编译器会进行类型推断
*/
//-----类型标注
let v3:Int;
//v3 = " ";错误,不能赋予字符串
v3 = 4;//正确
var v4:Int;
//var v5;错误,没有初始化,也没有指定类型
/*
3.输出
(1).避免空行输出
(2).“+”连接字符输出
(3).插值连接字符输出
*/
var v5 = "abc", v6:Float = 20.12,v7:Bool
let const1 = "xyz", const2:Double = 12.34;
//terminator: "" 避免输出空行
print(const2, terminator: "")
print("")
var num = 20
let name:String = "iMac"
let isValue:Bool = true
//“+”连接字符,非String类型要先转化为String类型
print("num:" + String(num) + " name:" + name + " isValue:" + String(isValue))
//插值连接字符
let description = "num:\(num) name:\(name) isValue:\(isValue)"
print(description)
//输出结果:num:20 name:iMac isValue:true
import Foundation
/**
* 1.整型:有符号和无符号
有符号:Int、Int8、Int16、Int32、Int64。
32位:Int = Int32
64位:Int = Int64
无符号:UInt、UInt8、UInt16、UInt32、UInt64。
32位:UInt = UInt32
64位:UInt = UInt64
*/
let minIntValue = Int.min;
let maxIntValue = Int.max;
let minInt8Value = Int8.min;
let maxInt8Value = Int8.max;
let maxInt1 = INT_MAX;
let maxInt2 = Int32.max;
print("minIntValue=\(minIntValue),maxIntValue=\(maxIntValue)");
print("minInt8Value=\(minInt8Value), maxInt8Value=\(maxInt8Value)")
print("maxInt1=\(maxInt1), maxInt2=\(maxInt2)");
/**
* minIntValue=-9223372036854775808,maxIntValue=9223372036854775807
minInt8Value=-128, maxInt8Value=127
maxInt1=2147483647, maxInt2=2147483647
*/
let decimalInteger = 17
let binaryInteger = 0b10001 // 二进制的17
let octalInteger = 0o21 // 八进制的17
let hexadecimalInteger = 0x11 // 十六进制的17
/**
* 2.浮点型
*/
let value1 = 123.4; // 浮点默认为double
let value2:Float = 33.2; // float
let decimalDouble = 12.1875
let exponentDouble = 1.21875e1
print("decimalDouble=\(decimalDouble), exponentDouble=\(exponentDouble)");
// 可读性
let value5 = 12_000_000
let value6 = 1_000_000.000_000_1;
let a = 1_0_0_1
/**
* 3.布尔型
false、true
*/
var isTrue = true
var isFalse = false
/**
* 4.数值转换
不同类型的数不能直接相加,要进行类型转换
*/
let value7:Int32 = 12
let value8:Int16 = 12
//let value9 = value7 + value8 要类型转换
let value9 = value7 + Int32(value8)
/**
* 5.定义类型别名
swift有很多类型别名,之后会慢慢提到
*/
typealias NewType = Int32
var new_value:NewType = 123
import Foundation
/**
* 1.字符与字符串
(1).字符与字符串都是用双引号
(2).如果不指定类型,默认为字符串
*/
let charValue1:Character = Character("a");
var charValue2:Character = "上"
var strValue1:String = "abcd"
let strValue2:String = String("变形金刚5")
/**
* 2.枚举所有字符
(1).连接字符串可以用"+"
*/
let charCollection = "获取字符串中的每一个字符"
for c in charCollection.characters
{
print("<\(c)>",terminator: "")
//<获><取><字><符><串><中><的><每><一><个><字><符>
}
print()
// 输出字符的unicode编码
let strValue3 = "abcd上下左右"
for c in strValue3.unicodeScalars
{
print("\(c.value) ",terminator: "")
//97 98 99 100 19978 19979 24038 21491
}
print(charCollection+strValue3)
/**
* 3.字符串之间的比较
直接使用==,>,<进行比较
*/
var strValueA = "abcd"
var strValueB = "abce"
if strValueA > strValueB
{
print("strValueA > strValueB");
}
else if strValueA < strValueB
{
print("strValueA < strValueB");
}
else
{
print("strValueA = strValueB");
}
// 大小写转换
let lowercaseStr = "abcdefg"
let uppercaseStr = lowercaseStr.uppercaseString
print(uppercaseStr);
print(uppercaseStr.lowercaseString)
import Foundation
/**
* 1.元组常量/变量的定义
*/
let product1 = (20, "iPhone6", 5888);
let product_name = "iPhone7"
var product2 = (30, "\(product_name)", 18888)
print(product1);
print(product2);
/**
* 2.从元组数据中提取每一个元素的值
*/
let (id1, name1, price1) = product2
print("id1=\(id1), name1 = \(name1), price1=\(price1)")
var (id, name, price) = product1;
print("id=\(id) name=\(name) price=\(price)");
//只取元组中的一个值
let (_,name2,_) = product1
print("name2=\(name2)")
/**
* 3.为元组数据中的每一个元素命名
*/
let product3 = (30, name:"iPhone8", price:5988)
print(product3.name)
//可选类型
//?表示不确定是否有值
//!表示确定有值
var numStr:String = "123a"
var value:Int? = Int(numStr);
if value != nil
{
print("转换成功")
}
//“123a”,错误,"123"正确
var value1:Int! = Int(numStr);
if value1 != nil
{
print("转换成功")
print(value1);
}else
{
print("转换失败")
// print(value1); 错误
}
//与OC相比较为特殊的运算符
//空合运算符 ??
let defaultColorName = "red"
var userDefinedColorName: String? //默认值为 nil
var colorNameToUse = userDefinedColorName ?? defaultColorName
// userDefinedColorName 的值为空,所以 colorNameToUse 的值为 "red"
userDefinedColorName = "green"
colorNameToUse = userDefinedColorName ?? defaultColorName
// userDefinedColorName 非空,因此 colorNameToUse 的值为 "green"
//区间运算符
//闭区间 ...
for index in 1...5 {
print("\(index) * 5 = \(index * 5)")
}
// 1 * 5 = 5
// 2 * 5 = 10
// 3 * 5 = 15
// 4 * 5 = 20
// 5 * 5 = 25
//半开半闭 ..<
let names = ["Anna", "Alex", "Brian", "Jack"]
let count = names.count
for i in 0..<count {
print("第 \(i + 1) 个人叫 \(names[i])")
}
// 第 1 个人叫 Anna
// 第 2 个人叫 Alex
// 第 3 个人叫 Brian
// 第 4 个人叫 Jack