let和var
var a = 10
let b = 20
print(a,b)
a = 30
print(a)
b = 50
基础数据类型
- Int、Float、Double、Bool、Character、String
- Array、Dictionary、(元组类型)Tuple
- 数据类型的首字母都是大写、两个类型不相同的数值,是不能直接进行运算的
类型别名
typealias SK = Int
let i:SK = 666
print(i)
typealias Block = (_ age:Int)->()
let myBlock : Block
类型转换
let str: String = "10"
let a = Int(str)
let str: String = "10"
let a = Double(str)
let a: Int = 20
let str = String(a)
let a: Int = 20
let b = Double(a)
let a: Double = 3.14
let str = String(stringInterpolationSegment: a)
let a: Double = 3.14
let b = Int(a)
??语法
let defaultColorName = "red"
var userDefinedColorName: String?
var colorNameToUse = userDefinedColorName ?? defaultColorName
可选类型以及可选类型取值
var newName : String?
newName = "swift"
print(newName)
let dictionary = ["name":"objective"]
let item = dictionary["name"]
print(item)
print(newName!,item!)
let itemS = dictionary["nil"]
print(itemS)
if let item = item {
print(item)
}
print(item ?? "默认值")
guard let new = item else {
return
}
print(new)
元组
var yz = ("he",true,"swift",15)
let h :(Int,String) = (66,"swift")
let c = (age:10,name:"swift")
let (age,name) = (22,"sk")
let itemYZ = yz.1
let itemH = h.1
let itemC = c.name
yz.1 = false
print(age,name)
print(itemYZ,itemH,itemC)
数组
let array = ["1","2","3"]
var mArray = Array<String>()
var selectArray : Array<Dictionary<String,Any>> = []
mArray.append("First")
mArray.append("Second")
mArray.append("Third")
print(array,mArray)
mArray.remove(at: 1)
print(mArray)
let item = mArray[0]
print(item)
mArray[0] = "edit First"
print(mArray)
for item in mArray {
print(item)
}
for item in mArray[0..<1] {
print(item)
}
for (index,value) in mArray.enumerated() {
print(index,value)
}
let watch_list = NSMutableArray.init()
let array = CommonDataManage.sharedManager().watchBindArray
if let modelArray = array as? [ContectWatchModel] {
for model in modelArray {
watch_list.add(["watch_userid":NSNumber.init(value: Int(truncating: model.watch_userid))])
}
}
字典
let dic:[String:Any] = ["name":"s","age":10]
let dictionary = ["name":"swift","age":"10"]
print(dic,dictionary)
var dicM = [String : Any]()
dicM["height"] = 20
dicM["name"] = "objective"
print(dicM)
dicM.removeValue(forKey: "name")
print(dicM)
dicM["height"] = 178
print(dicM)
let item = dicM["height"]
print(item!)
dicM["weight"] = 108
dicM["age"] = 28
for key in dicM.keys {
print(key)
}
for value in dicM.values {
print(value)
}
for (key,value) in dicM {
print(key)
print(value)
}
字符串
var string = ""
print(string)
if string.isEmpty {
print("空字符串!")
}
string = "swift"
print(string)
var str : String?
str = "objective"
print(str ?? "")
let str_1 = "a little"
print(str_1)
let softWrappedQuotation = """
1. 我是swift
2. 你好好的生活着
3. 理解万岁
"""
print(softWrappedQuotation)
let open_swift = "openswift"
for c in open_swift {
print(c)
}
let a = 10
let c:String = "hello"
let h:String = c + "\(a)"
print(h)
注释
闭包
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let sum = {
(num1: Int, num2: Int) -> Int in
return num1 + num2
}
let result = sum(5,5)
print(result)
callBck { (dictionary) in
print(dictionary)
}
callBck { (dic) in
print(dic)
}
}
typealias SumBlock = (Dictionary<String, String>) -> ()
func callBck(block:SumBlock)
{
let dictionary = ["key" : "10"]
block(dictionary)
}
}
函数
func initUI(title : String) {
}
func callTitle(title : String) -> String {
return title + "改变!"
}
func blindWatch() {
}
func callName(_ name:String...) {
var totalStr = ""
for str in name {
totalStr += str
print(str)
}
print(totalStr)
}
func sd_change(name : inout String) {
name = "new_" + name
print(name)
}
枚举
import UIKit
enum PustType {
case commonPushType
case historyPushType
case recordPushType
}
enum Week: Int{
case MON, TUE, WED, THU, FRI, SAT, SUN
}
enum Season {
case spring,summer,autumn,winter
}
enum Shape{
case circle(radius: Double)
case rectangle(width: Int, height: Int)
}
enum Code{
case num(Int,Int,Int)
case str(String,String)
}
class ViewController: UIViewController {
var type : Shape?
override func viewDidLoad() {
super.viewDidLoad()
switch self.type {
case .circle:
print("the shape is circle")
case .rectangle:
print("the shape is rectangle")
case .none:
print("nil")
}
let code = Code.str("A", "B")
print(code)
let week = Week(rawValue: 2)
print(week ?? "nil")
}
}
可选项绑定
let dic :Dictionary<String,String>? = ["key":"value"]
print(dic as Any)
if let dic = dic,let value = dic["key"] {
print(dic)
print(value)
}
let adress: String? = "杭州"
print(adress)
if let adress = adress {
print(adress)
}
let a: Int? = 10
guard let value = a else {
return
}
print(value)
guard语句
let dictionary = ["name":"swift"]
let item = dictionary["name"]
print(item)
guard let new = item else {
return
}
print(new)
if语句
let dic : Dictionary<String,String> = ["name":"Tom","age":"18","id":"60018"]
let name = dic["name"]
if name == "Tom" {
print("找到了Tom")
}else if(name == "cat")
{
print("找到了cat")
}else
{
print("没找到了cat和Tom")
}
swich语句
let watch_id : Int? = 2
switch watch_id {
case 0:
print("ID 为 0")
case 1:
print("ID 为 1")
case 2,3:
print("ID 为 2 或者 为3")
fallthrough
default:
print("case")
}
let somePoint = (1, -1)
switch somePoint {
case let(x, y) where x == y:
print("1")
case let(x, y) where x == -y:
print("2")
case let(x, y):
print("3")
}
三目运算符
let a = 20
let b = 30
let c = a > b ? a : b
print(c)
for语句
let array = ["健康","开心","气质","自信","富有","平安","长寿"]
for title in array {
print(title)
}
let array_two : [Int] = [10, 20, 30]
for id in array_two {
print( "index 的值为 \(id)")
}
属性
import UIKit
var allCan : String = "my_swif"
class ViewController: UIViewController {
var age : Int = 18
var nurse_userid : String?
var width: Double = 8.0
var area: Double {
get {
return width * width
}
set (newValue){
width = sqrt(newValue)
}
}
var width_new: CGFloat {
get { return self.view.frame.size.width }
set { self.view.frame.size.width = newValue }
}
lazy var ske_id : String = "60001"
static var weight: Int = 115
override func viewDidLoad() {
super.viewDidLoad()
self.nurse_userid = "600028"
print(self.age,self.nurse_userid ?? "123")
print("面积是:" + "\(self.area)")
print("屏幕宽度是:" + "\(self.width_new)")
print(self.ske_id)
}
}
属性监听器
var counter: Int = 0{
willSet(newTotal){
print("willSet: \(newTotal)")
}
didSet{
if counter > oldValue {
print("didSet \(counter - oldValue)")
}
}
}
counter = 20
counter = 88