1940 字
预计 10 分钟阅读完
Swift 编程语言学习笔记
Swift 编程语言学习笔记
Swift 是 Apple 于 2014 年推出的现代编程语言,用于开发 iOS、macOS、watchOS 和 tvOS 应用。
基础语法
变量与常量
// 使用 let 声明常量let maxLoginAttempts = 10let welcomeMessage: String = "Hello, Swift!"
// 使用 var 声明变量var currentLoginAttempt = 0var x = 0.0, y = 0.0, z = 0.0
// 类型推断let meaningOfLife = 42 // Intlet pi = 3.14159 // Doublelet anotherPi = 3 + 0.14159 // Double数据类型
// 整数类型let minValue = UInt8.min // 0let maxValue = UInt8.max // 255
// 浮点数let floatValue: Float = 3.14let doubleValue = 3.14159265359
// 字符串let name = "Swift"let greeting = "Hello, \(name)!" // 字符串插值
// 数组var skills = ["Python", "JavaScript"]skills.append("Swift")let immutableArray = [1, 2, 3]
// 字典var userInfo: [String: Any] = [ "name": "John", "age": 25, "isDeveloper": true]userInfo["city"] = "Beijing"
// 集合var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]favoriteGenres.insert("Jazz")控制流
条件语句
// if 语句let temperature = 22if temperature <= 18 { print("天气较凉")} else if temperature >= 30 { print("天气炎热")} else { print("天气舒适")}
// 三元运算符let status = temperature >= 25 ? "温暖" : "凉爽"
// switch 语句let fruit = "Apple"switch fruit {case "Apple": print("这是一个苹果")case "Banana": print("这是一根香蕉")case "Orange", "Mango": // 多个值匹配 print("这是橙子或芒果")default: print("未知水果")}
// 范围匹配let score = 85switch score {case 0..<60: print("不及格")case 60..<80: print("及格")case 80..<90: print("良好")case 90...100: print("优秀")default: print("超出范围")}循环语句
// for-in 循环for i in 1...5 { print(i) // 1, 2, 3, 4, 5}
for fruit in ["Apple", "Banana", "Orange"] { print(fruit)}
for (index, item) in ["A", "B", "C"].enumerated() { print("Index: \(index), Value: \(item)")}
// while 循环var counter = 0while counter < 5 { counter += 1 print(counter)}
// repeat-while (do-while)repeat { print("至少执行一次")} while false函数
基本函数
// 无参数无返回值func sayHello() { print("Hello!")}
// 有参数func greet(name: String) -> String { return "Hello, \(name)!"}
// 多参数func calculateArea(width: Double, height: Double) -> Double { return width * height}
// 无标签参数func sum(_ a: Int, _ b: Int) -> Int { return a + b}
// 参数标签func introduce(person name: String, from city: String) { print("\(name) 来自 \(city)")}
introduce(person: "张三", from: "北京")
// 默认参数func log(message: String, level: String = "INFO") { print("[\(level)] \(message)")}
log(message: "程序启动") // [INFO] 程序启动log(message: "发生错误", level: "ERROR")函数类型
// 函数类型作为参数func operation(a: Int, b: Int, function: (Int, Int) -> Int) -> Int { return function(a, b)}
let add: (Int, Int) -> Int = { $0 + $1 }let multiply: (Int, Int) -> Int = { $0 * $1 }
print(operation(a: 5, b: 3, function: add)) // 8print(operation(a: 5, b: 3, function: multiply)) // 15
// 函数类型作为返回值func chooseOperation(operator op: String) -> (Int, Int) -> Int { switch op { case "+": return { $0 + $1 } case "*": return { $0 * $1 } default: return { $0 - $1 } }}
let chooseAdd = chooseOperation(operator: "+")print(chooseAdd(10, 20)) // 30闭包
// 基本语法let greetClosure = { (name: String) in return "Hello, \(name)"}
// 尾随闭包numbers.map { $0 * 2 }
// 捕获值func makeIncrementer(forIncrement amount: Int) -> () -> Int { var runningTotal = 0 func incrementer() -> Int { runningTotal += amount return runningTotal } return incrementer}
let incrementByTwo = makeIncrementer(forIncrement: 2)print(incrementByTwo()) // 2print(incrementByTwo()) // 4print(incrementByTwo()) // 6面向对象
类与结构体
// 结构体 - 值类型struct Point { var x: Double var y: Double
init(x: Double, y: Double) { self.x = x self.y = y }
func distance(to other: Point) -> Double { let dx = other.x - x let dy = other.y - y return sqrt(dx * dx + dy * dy) }}
// 类 - 引用类型class Vehicle { var currentSpeed = 0.0 let maxSpeed: Double
init(maxSpeed: Double) { self.maxSpeed = maxSpeed }
func description() -> String { return "当前速度: \(currentSpeed) km/h" }
// 子类可重写 func makeSound() { // 默认实现 }}
class Car: Vehicle { var brand: String
init(brand: String, maxSpeed: Double) { self.brand = brand super.init(maxSpeed: maxSpeed) }
override func makeSound() { print("嘟嘟嘟~") }
// 属性观察者 var speedInKmh: Double { get { return currentSpeed } set { currentSpeed = newValue } }}继承与多态
class Shape { var area: Double { return 0 }
func describe() { print("这是一个图形") }}
class Rectangle: Shape { var width: Double var height: Double
init(width: Double, height: Double) { self.width = width self.height = height }
override var area: Double { return width * height }
override func describe() { print("这是一个矩形, 面积: \(area)") }}
class Circle: Shape { var radius: Double
init(radius: Double) { self.radius = radius }
override var area: Double { return .pi * radius * radius }}
// 多态let shapes: [Shape] = [ Rectangle(width: 5, height: 3), Circle(radius: 2)]
for shape in shapes { shape.describe()}协议
// 定义协议protocol Drawable { func draw() var lineColor: String { get set }}
protocol Movable { func move(to point: Point)}
// 协议继承protocol ShapeProtocol: Drawable, Movable { var area: Double { get }}
// 类遵守协议class CircleShape: ShapeProtocol { var center: Point var radius: Double var lineColor: String
init(center: Point, radius: Double, lineColor: String) { self.center = center self.radius = radius self.lineColor = lineColor }
func draw() { print("画圆, 圆心: (\(center.x), \(center.y)), 半径: \(radius)") }
func move(to point: Point) { center = point print("圆移动到: (\(point.x), \(point.y))") }
var area: Double { return .pi * radius * radius }}
// 协议扩展extension Drawable { func drawWithBorder() { print("边框颜色: \(lineColor)") draw() }}泛型
// 泛型函数func swapValues<T>(_ a: inout T, _ b: inout T) { let temp = a a = b b = temp}
var a = 1, b = 2swapValues(&a, &b)print("a = \(a), b = \(b)") // a = 2, b = 1
// 泛型类型struct Stack<Element> { private var items: [Element] = []
mutating func push(_ item: Element) { items.append(item) }
mutating func pop() -> Element? { return items.popLast() }
var isEmpty: Bool { return items.isEmpty }}
var intStack = Stack<Int>()intStack.push(1)intStack.push(2)print(intStack.pop()) // 2
// 泛型协议protocol Container { associatedtype Item mutating func append(_ item: Item) var count: Int { get } subscript(i: Int) -> Item { get }}错误处理
// 定义错误类型enum DataError: Error { case notFound case unauthorized case invalidData(String) case networkError(Int)}
// 抛出错误func fetchData(from url: String) throws -> Data { guard url.hasPrefix("https://") else { throw DataError.invalidData("URL 必须以 https:// 开头") }
// 模拟网络请求 throw DataError.notFound}
// 处理错误do { let data = try fetchData(from: "http://example.com")} catch DataError.notFound { print("数据未找到")} catch DataError.unauthorized { print("未授权访问")} catch let DataError.invalidData(message) { print("无效数据: \(message)")} catch { print("发生未知错误: \(error)")}
// try? - 将错误转换为可选值let result = try? fetchData(from: "https://example.com")// 如果出错, result 为 nil
// try! - 断言不会出错 (慎用!)let forcedResult = try! fetchData(from: "https://example.com")
// defer - 延迟执行func processFile() throws { let file = openFile() defer { closeFile(file) // 无论是否出错,都会执行 }
try writeData(to: file) try anotherOperation()}可选链与空合运算符
// 可选链class Person { var residence: Residence?}
class Residence { var address: Address?}
class Address { var street: String = "" var city: String = "" var zipCode: String?}
let john = Person()let street = john.residence?.address?.street // 如果任何环节为 nil, 返回 nil
// 空合运算符 ??let defaultCity = "北京"let city = john.residence?.address?.city ?? defaultCity
// 可选绑定if let street = john.residence?.address?.street { print("街道: \(street)")} else { print("地址信息不完整")}
// 隐式展开可选var assumedString: String! = nil// 直接使用,无需解包let implicitString: String = assumedString内存管理
// 强引用循环class Person { var name: String var apartment: Apartment?
init(name: String) { self.name = name }
deinit { print("\(name) 被释放") }}
class Apartment { var tenant: Person?
deinit { print("公寓被释放") }}
// 解决: 使用 weak 或 unownedclass Person2 { var name: String weak var apartment: Apartment2? // weak
init(name: String) { self.name = name }}
class Apartment2 { var tenant: Person2? unowned let building: Building // unowned
init(building: Building) { self.building = building }}访问控制
// open - 最高权限,可跨模块访问和继承open class OpenClass {}
// public - 跨模块访问,不可继承public class PublicClass {}
// internal - 默认,模块内访问class InternalClass {}
// fileprivate - 文件内访问fileprivate class FilePrivateClass {}
// private - 当前作用域内访问private class PrivateClass { private var secret: String = "机密"
func access() { let secret2 = secret // 可以访问 }}总结
Swift 是一门现代、安全、高效的编程语言:
- 安全性: 强类型系统、可选类型、自动内存管理
- 现代性: 语法简洁、支持泛型、函数式编程特性
- 高性能: 编译型语言,性能接近 C++
- 易用性: 丰富的标准库、优秀的错误处理机制
掌握 Swift 的核心概念是开发 Apple 平台应用的基础。
参考资源
Swift 编程语言学习笔记
https://fuwari.vercel.app/posts/swift-learning-notes/