# Swift — Apple Modern Programming Language > Swift is a powerful and intuitive programming language for building apps for iOS, macOS, watchOS, tvOS, Linux, and the server. Created at Apple to replace Objective-C. Fast, safe, and expressive with protocol-oriented design. ## Install Save in your project root: ## Quick Use ```bash # macOS: comes with Xcode xcode-select --install # Linux # Download from https://www.swift.org/download # Or via swiftly version manager curl -L https://swift-server.github.io/swiftly/swiftly-install.sh | bash swiftly install latest swift --version ``` Hello world: ```swift // main.swift import Foundation let users = [ (name: "Alice", score: 95), (name: "Bob", score: 87), (name: "Carol", score: 91), ] let sorted = users.sorted { $0.score > $1.score } for user in sorted { print("\(user.name): \(user.score)") } ``` Swift Package Manager: ```bash mkdir MyCLI && cd MyCLI swift package init --type executable swift build swift run swift test swift package update ``` ## Intro Swift is a powerful and intuitive programming language developed by Apple, first announced at WWDC 2014. Designed to replace Objective-C while remaining interoperable with it. Swift is fast (compiled to native code via LLVM), safe (eliminates entire classes of bugs with optionals, value types, and memory-safety features), and expressive (modern syntax, closures, generics, protocol-oriented programming). - **Repo**: https://github.com/swiftlang/swift (formerly apple/swift) - **Stars**: 69K+ - **License**: Apache 2.0 ## What Swift Does - **Native compilation** — LLVM-based, native ARM/x86/WASM - **Optionals** — no nil pointer crashes - **Value types** — structs, enums with associated values - **Protocols** — structural polymorphism, protocol extensions - **Generics** — parameterized types and functions - **Closures** — first-class, trailing syntax - **Concurrency** — async/await + actors (Swift 5.5+) - **SwiftUI** — declarative UI framework - **Swift Package Manager** — built-in dep/build tool - **Interop** — Objective-C, C, and C++ interop - **Cross-platform** — macOS, iOS, watchOS, tvOS, Linux, Windows ## Architecture Swift compiler built on LLVM. Uses Automatic Reference Counting (ARC) for memory management — no GC pauses. Modules and package format replace Objective-C frameworks. SwiftUI uses result builders and property wrappers for declarative UI. ## Self-Hosting Language toolchain. ## Key Features - Protocol-oriented programming - Value types and copy-on-write - Async/await and actors - Swift Concurrency (structured concurrency) - Result builders for DSLs - Property wrappers - Swift Package Manager - Server-side frameworks (Vapor, Hummingbird) - Interop with Objective-C and C++ - Linux and Windows support ## Comparison | Language | Memory | Concurrency | Interop | |---|---|---|---| | Swift | ARC | async + actors | ObjC, C, C++ | | Rust | Ownership | async | C, C++ | | Kotlin | GC (JVM) | Coroutines | Java | | C# | GC | async/await | .NET | | Go | GC | Goroutines | C (cgo) | ## 常见问题 FAQ **Q: 只能 Apple 平台?** A: 不。Swift 是跨平台开源语言,官方支持 macOS、iOS、Linux、Windows。Vapor、Hummingbird 等 server framework 用于后端。 **Q: 和 Objective-C 关系?** A: Swift 完全兼容 ObjC(互调、共享项目)。Apple 新 API 优先 Swift。老项目可以渐进迁移。 **Q: Swift Concurrency 能取代 GCD 吗?** A: 大部分场景可以。async/await + actor 解决数据竞争。GCD 仍然是底层工具,但高层代码推荐 Swift Concurrency。 ## 来源与致谢 Sources - Docs: https://www.swift.org/documentation - GitHub: https://github.com/swiftlang/swift - License: Apache 2.0 --- Source: https://tokrepo.com/en/workflows/7cce7840-3606-11f1-9bc6-00163e2b0d79 Author: AI Open Source