iOS 开发入门指南
目录
- 项目结构解析
- [iOS vs Android 对比](#iOS vs Android 对比)
- 核心语法科普
- [AppDelegate 与 SceneDelegate](#AppDelegate 与 SceneDelegate)
- 开发工具与快捷键
- 总结与建议
项目结构解析
HelloWorldApp 项目目录结构
HelloWorldApp/
├── HelloWorldApp/ # 源代码目录
│ ├── AppDelegate.swift # 应用程序委托
│ ├── SceneDelegate.swift # 场景委托 (iOS 13+)
│ ├── Controllers/
│ │ └── ViewController.swift # 视图控制器
│ ├── Assets.xcassets/ # 资源文件
│ ├── Base.lproj/
│ │ └── Main.storyboard # 故事板文件
│ └── Info.plist # 应用配置文件
├── HelloWorldApp.xcodeproj/ # Xcode 项目文件
└── HelloWorldApp.xcworkspace/ # Xcode 工作空间文件
目录作用说明
目录/文件 | 作用 | Android 对应 |
---|---|---|
HelloWorldApp/ |
源代码和资源文件 | app/src/main/ |
HelloWorldApp.xcodeproj/ |
项目配置和构建设置 | .idea/ + build.gradle |
HelloWorldApp.xcworkspace/ |
工作空间配置(多项目管理) | settings.gradle |
Assets.xcassets/ |
图片、颜色等资源 | res/drawable/ |
Info.plist |
应用元数据配置 | AndroidManifest.xml |
iOS vs Android 对比
架构对比
概念 | iOS (Swift) | Android (Kotlin/Java) |
---|---|---|
应用入口 | AppDelegate |
Application 类 |
界面控制器 | ViewController |
Activity |
布局方式 | 代码布局 + Auto Layout | XML布局 + ConstraintLayout |
生命周期管理 | viewDidLoad , viewWillAppear |
onCreate , onStart , onResume |
项目管理对比
iOS 特点:
- Xcode 隐藏技术细节,专注代码开发
.xcodeproj
和.xcworkspace
在 Finder 中可见,Xcode 中隐藏- 统一的开发环境和工具链
Android 特点:
- 项目结构完全可见和可编辑
- 多种 IDE 选择(Android Studio、IntelliJ IDEA)
- Gradle 构建系统更加透明
核心语法科普
1. 应用入口:AppDelegate
swift
@main // 相当于Android的Application类
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow? // 相当于Android的根Activity
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// 相当于Android Application的onCreate()
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = ViewController() // 设置根控制器
window?.makeKeyAndVisible()
return true
}
}
Android 对比:
kotlin
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
// 应用初始化
}
}
2. 界面控制器:ViewController
类声明和继承
swift
// iOS
class ViewController: UIViewController {
// 相当于Android的Activity
}
kotlin
// Android
class MainActivity : AppCompatActivity() {
// iOS的ViewController等价物
}
UI组件声明 - 闭包 vs 传统初始化
iOS 使用闭包初始化UI组件:
swift
private let helloLabel: UILabel = {
let label = UILabel() // 创建实例
label.text = "Hello, World!" // 设置属性
label.font = UIFont.systemFont(ofSize: 32, weight: .bold)
label.textAlignment = .center
label.textColor = .systemBlue
label.translatesAutoresizingMaskIntoConstraints = false // 关闭自动布局转换
return label // 返回配置好的实例
}() // 立即执行闭包
Android 对比:
kotlin
// 方式一:XML中定义 + 代码引用
<TextView
android:id="@+id/helloLabel"
android:text="Hello, World!"
android:textSize="32sp"
android:textAlignment="center"
android:textColor="@color/blue" />
// 在Activity中引用
private lateinit var helloLabel: TextView
// 方式二:纯代码创建(类似iOS)
private val helloLabel: TextView by lazy {
TextView(this).apply {
text = "Hello, World!"
textSize = 32f
textAlignment = TextView.TEXT_ALIGNMENT_CENTER
setTextColor(ContextCompat.getColor(context, R.color.blue))
}
}
iOS vs Android 组件对比详解
特性 | iOS (UIKit) | Android (View System) |
---|---|---|
组件创建 | 闭包初始化 {}() |
XML声明 + findViewById 或 by lazy |
属性设置 | 点语法 label.text = "..." |
XML属性或代码设置 setText("...") |
布局系统 | Auto Layout 约束 | LinearLayout/ConstraintLayout |
字体设置 | UIFont.systemFont() |
android:textSize 或 setTextSize() |
颜色系统 | .systemBlue 系统颜色 |
@color/blue 资源引用 |
内存管理 | ARC 自动管理 | GC 垃圾回收 |
常用UI组件对应关系
iOS组件 | Android组件 | 功能说明 |
---|---|---|
UILabel |
TextView |
文本显示 |
UIButton |
Button |
按钮控件 |
UITextField |
EditText |
文本输入 |
UIImageView |
ImageView |
图片显示 |
UIScrollView |
ScrollView |
滚动容器 |
UITableView |
RecyclerView |
列表视图 |
UICollectionView |
RecyclerView (Grid) |
网格视图 |
UISwitch |
Switch |
开关控件 |
UISlider |
SeekBar |
滑动条 |
UIProgressView |
ProgressBar |
进度条 |
UIAlertController |
AlertDialog |
弹窗对话框 |
UINavigationController |
Fragment + Navigation |
导航控制 |
组件初始化方式对比
闭包概念深度解析
什么是闭包?
闭包(Closure) 是一个可以捕获和存储其所在上下文中任意常量和变量引用的自包含的函数块。简单来说,闭包就是一个**"能记住周围环境的函数"**。
Swift 中的闭包语法
1. 基本语法结构:
swift
{ (参数列表) -> 返回类型 in
// 闭包体
return 结果
}
2. in
关键字的作用:
in
关键字是Swift闭包语法中的分隔符,用于分隔闭包的参数声明部分和执行体部分:
swift
{ (参数列表) -> 返回类型 in
// 闭包体 - 实际执行的代码
}
in
之前:闭包的"签名"部分(参数列表和返回类型)in
之后:闭包的"实现"部分(具体的执行代码)
语法规则:
- 当闭包有参数或指定返回类型时,
in
是必须的 - 当闭包无参数且无返回类型声明时,可以省略
in
in
总是在参数声明之后,执行体之前
3. 闭包的几种形式:
swift
// 完整形式
let fullClosure: (Int, Int) -> Int = { (a: Int, b: Int) -> Int in
return a + b
}
// 简化形式(类型推断)
let simpleClosure = { (a: Int, b: Int) in
return a + b
}
// 更简化(省略return)
let shorterClosure = { (a: Int, b: Int) in a + b }
// 最简化(使用$0, $1参数简写)
let shortestClosure = { $0 + $1 }
3. 立即执行闭包(IIFE - Immediately Invoked Function Expression):
swift
// 这就是UI组件初始化中使用的模式
private let result: Int = {
let a = 10
let b = 20
return a + b
}() // 注意这里的 () 表示立即执行
print(result) // 输出: 30
Kotlin 中的对应概念
Kotlin 有闭包概念吗? 答案是:有的,但叫法不同。
Kotlin 中对应的概念包括:
- Lambda 表达式
- 高阶函数
- 函数类型
- 匿名函数
Swift 闭包 vs Kotlin Lambda 对比
特性 | Swift 闭包 | Kotlin Lambda |
---|---|---|
基本语法 | { param in body } |
{ param -> body } |
参数简写 | $0, $1, $2 |
it (单参数时) |
类型声明 | (Int) -> String |
(Int) -> String |
捕获变量 | 自动捕获 | 自动捕获 |
立即执行 | { }() |
run { } |
实际代码对比
Swift - 闭包的各种用法:
swift
// 1. UI组件初始化(立即执行闭包)
private let titleLabel: UILabel = {
let label = UILabel()
label.text = "Hello"
label.font = UIFont.boldSystemFont(ofSize: 16)
return label
}()
// 2. 数组操作
let numbers = [1, 2, 3, 4, 5]
let doubled = numbers.map { $0 * 2 } // [2, 4, 6, 8, 10]
let filtered = numbers.filter { $0 > 3 } // [4, 5]
// 3. 异步回调
URLSession.shared.dataTask(with: url) { data, response, error in
// 处理网络响应
}
// 4. 事件处理
button.addAction(UIAction { _ in
print("按钮被点击")
}, for: .touchUpInside)
Kotlin - Lambda 的对应用法:
kotlin
// 1. 延迟初始化(类似立即执行闭包)
private val titleLabel: TextView by lazy {
TextView(this).apply {
text = "Hello"
setTypeface(null, Typeface.BOLD)
textSize = 16f
}
}
// 或使用 run 函数(更接近Swift的立即执行闭包)
private val titleLabel: TextView = run {
val label = TextView(this)
label.text = "Hello"
label.setTypeface(null, Typeface.BOLD)
label.textSize = 16f
label
}
// 2. 集合操作
val numbers = listOf(1, 2, 3, 4, 5)
val doubled = numbers.map { it * 2 } // [2, 4, 6, 8, 10]
val filtered = numbers.filter { it > 3 } // [4, 5]
// 3. 异步回调
retrofit.getData().enqueue(object : Callback<Data> {
override fun onResponse(call: Call<Data>, response: Response<Data>) {
// 处理响应
}
override fun onFailure(call: Call<Data>, t: Throwable) {
// 处理错误
}
})
// 或使用协程
viewModelScope.launch {
val data = repository.getData()
// 处理数据
}
// 4. 事件处理
button.setOnClickListener {
println("按钮被点击")
}
闭包的核心特性
1. 捕获变量(Variable Capture):
swift
// Swift
func makeIncrementer(incrementAmount: Int) -> () -> Int {
var total = 0
let incrementer: () -> Int = {
total += incrementAmount // 捕获了 total 和 incrementAmount
return total
}
return incrementer
}
let incrementByTwo = makeIncrementer(incrementAmount: 2)
print(incrementByTwo()) // 2
print(incrementByTwo()) // 4
kotlin
// Kotlin
fun makeIncrementer(incrementAmount: Int): () -> Int {
var total = 0
return {
total += incrementAmount // 捕获了 total 和 incrementAmount
total
}
}
val incrementByTwo = makeIncrementer(2)
println(incrementByTwo()) // 2
println(incrementByTwo()) // 4
2. 逃逸闭包 vs 非逃逸闭包:
swift
// Swift - 非逃逸闭包(默认)
func performOperation(_ operation: () -> Void) {
operation() // 在函数返回前执行
}
// Swift - 逃逸闭包
func performAsyncOperation(_ completion: @escaping () -> Void) {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
completion() // 在函数返回后执行
}
}
kotlin
// Kotlin - 所有lambda都可以"逃逸"
fun performOperation(operation: () -> Unit) {
operation() // 立即执行
}
fun performAsyncOperation(completion: () -> Unit) {
Handler(Looper.getMainLooper()).postDelayed({
completion() // 延迟执行
}, 1000)
}
UI组件初始化中的闭包优势
iOS - 闭包初始化的优势:
swift
// ✅ 优点:配置集中、类型安全、立即执行
private let customButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("点击我", for: .normal)
button.backgroundColor = .systemBlue
button.layer.cornerRadius = 8
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
// 🔄 等价的Android写法
private val customButton: Button by lazy {
Button(this).apply {
text = "点击我"
setBackgroundColor(ContextCompat.getColor(context, R.color.blue))
background.cornerRadius = 8.dpToPx()
}
}
为什么iOS偏爱闭包初始化?
- 配置集中:所有属性设置在一个地方
- 类型安全:编译时检查类型
- 立即执行:对象创建时就完成配置
- 代码清晰 :避免在
viewDidLoad
中大量UI配置代码 - 性能优化:只执行一次,之后直接使用
总结
闭包的本质:
- Swift的闭包和Kotlin的Lambda本质上都是**"函数式编程"**的体现
- 它们都能捕获上下文 、作为参数传递 、延迟执行
- 主要区别在于语法糖 和使用习惯
学习建议:
-
如果熟悉Kotlin的Lambda,理解Swift闭包会很容易
-
重点掌握立即执行闭包的UI初始化模式
-
理解逃逸闭包的概念,这在异步编程中很重要
Android - XML声明的优势:
xml<!-- ✅ 优点:可视化编辑、资源管理、主题适配 --> <Button android:id="@+id/customButton" android:text="点击我" android:background="@drawable/rounded_button" style="@style/PrimaryButton" />
事件处理对比
iOS - Target-Action 模式详解:
核心概念解释:
- Target :目标对象(通常是
self
,即当前视图控制器) - Action :要执行的方法(通过
#selector
指定) - for:触发事件的类型(如点击、长按等)
swift
// 方式一:addTarget(传统方式)
customButton.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
// ↑ ↑ ↑
// 目标对象 要执行的方法 触发事件类型
@objc private func buttonTapped() {
print("按钮被点击")
}
// 方式二:闭包(iOS 14+,现代方式)
customButton.addAction(UIAction { _ in
print("按钮被点击")
}, for: .touchUpInside)
详细参数解释:
1. #selector
是什么?
#selector
是Swift中的选择器语法,用于将方法名转换为Objective-C可识别的选择器- 相当于告诉系统:"当事件发生时,调用这个方法"
- 被
#selector
引用的方法必须标记为@objc
2. for
参数 - 事件类型:
事件类型 | 含义 | Android对应 |
---|---|---|
.touchUpInside |
手指在按钮内部抬起(标准点击) | OnClickListener |
.touchDown |
手指按下 | OnTouchListener.ACTION_DOWN |
.touchUpOutside |
手指在按钮外部抬起 | 无直接对应 |
.valueChanged |
值改变(如滑块、开关) | OnSeekBarChangeListener |
.editingChanged |
文本编辑中 | TextWatcher.afterTextChanged |
.editingDidEnd |
文本编辑结束 | OnFocusChangeListener |
3. 完整的事件绑定流程:
swift
// 步骤1:创建按钮
let button = UIButton(type: .system)
button.setTitle("点击我", for: .normal)
// 步骤2:绑定事件(Target-Action模式)
button.addTarget(self, // 目标:当前控制器
action: #selector(handleButtonTap), // 动作:要执行的方法
for: .touchUpInside) // 事件:手指在按钮内抬起
// 步骤3:实现响应方法
@objc private func handleButtonTap() {
print("按钮被点击了!")
// 处理点击逻辑
}
Android - 监听器模式:
kotlin
// 方式一:setOnClickListener
customButton.setOnClickListener {
println("按钮被点击")
}
// 方式二:XML中声明
// android:onClick="buttonTapped"
fun buttonTapped(view: View) {
println("按钮被点击")
}
布局系统深度对比
iOS - Auto Layout 约束系统:
重要概念:view
是什么?
view
是UIViewController
的根视图属性,相当于Android中的父容器- 它代表整个屏幕的主视图区域(除了状态栏、导航栏等系统UI)
- 所有子视图都添加到这个
view
上,并相对于它进行布局约束
视图层级关系:
屏幕
├── 状态栏 (系统)
├── 导航栏 (可选)
└── view (ViewController的根视图) ← 约束代码中的 view
├── helloLabel
├── button
└── 其他子视图...
swift
// 代码方式设置约束
helloLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
// ↑
// ViewController的根视图
helloLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -50).isActive = true
helloLabel.widthAnchor.constraint(equalToConstant: 200).isActive = true
helloLabel.heightAnchor.constraint(equalToConstant: 50).isActive = true
// 或使用 NSLayoutConstraint
NSLayoutConstraint.activate([
helloLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
helloLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 100)
// ↑
// 安全区域(避开刘海、底部指示器)
])
iOS vs Android 对比:
iOS | Android | 含义 |
---|---|---|
view |
parent |
父容器/根布局 |
view.centerXAnchor |
parent (水平居中) |
父容器的水平中心 |
view.safeAreaLayoutGuide |
无直接对应 | 安全区域布局指南 |
**Android - ConstraintLayout 约束系统:**
```xml
<!-- XML方式设置约束 -->
<androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="@+id/helloLabel"
android:layout_width="200dp"
android:layout_height="50dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
kotlin
// 代码方式设置约束
val constraintSet = ConstraintSet()
constraintSet.connect(helloLabel.id, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 100)
constraintSet.connect(helloLabel.id, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START)
constraintSet.connect(helloLabel.id, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END)
constraintSet.applyTo(constraintLayout)
样式和主题管理对比
iOS - 外观代理和样式:
swift
// 全局样式设置
UILabel.appearance().font = UIFont.systemFont(ofSize: 16)
UIButton.appearance().tintColor = .systemBlue
// 个别组件样式
helloLabel.layer.cornerRadius = 8
helloLabel.layer.borderWidth = 1
helloLabel.layer.borderColor = UIColor.systemGray.cgColor
helloLabel.backgroundColor = .systemBackground
Android - 样式和主题系统:
xml
<!-- styles.xml -->
<style name="CustomLabelStyle">
<item name="android:textSize">16sp</item>
<item name="android:textColor">@color/primary_text</item>
<item name="android:background">@drawable/rounded_background</item>
</style>
<!-- 应用样式 -->
<TextView
style="@style/CustomLabelStyle"
android:text="Hello, World!" />
数据绑定和状态管理对比
iOS - 属性观察器和KVO:
swift
class ViewController: UIViewController {
@IBOutlet weak var countLabel: UILabel!
var count: Int = 0 {
didSet {
countLabel.text = "计数: \(count)"
}
}
// 或使用 Combine (iOS 13+)
@Published var count: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
$count
.map { "计数: \($0)" }
.assign(to: \.text, on: countLabel)
.store(in: &cancellables)
}
}
Android - 数据绑定和LiveData:
kotlin
// 使用 LiveData 观察数据变化
class MainActivity : AppCompatActivity() {
private val viewModel: MainViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// 观察数据变化
viewModel.count.observe(this) { count ->
findViewById<TextView>(R.id.countLabel).text = "计数: $count"
}
}
}
导航和页面跳转对比
iOS - Navigation Controller:
swift
// 推入新页面
let detailVC = DetailViewController()
navigationController?.pushViewController(detailVC, animated: true)
// 模态展示
let modalVC = ModalViewController()
present(modalVC, animated: true)
// 使用 Storyboard Segue
performSegue(withIdentifier: "showDetail", sender: self)
Android - Intent 和 Fragment:
kotlin
// Activity 跳转
val intent = Intent(this, DetailActivity::class.java)
intent.putExtra("data", "传递的数据")
startActivity(intent)
// Fragment 切换
supportFragmentManager.beginTransaction()
.replace(R.id.fragment_container, DetailFragment())
.addToBackStack(null)
.commit()
// 使用 Navigation Component
findNavController().navigate(R.id.action_main_to_detail)
核心差异总结
方面 | iOS 特点 | Android 特点 |
---|---|---|
UI创建 | 代码优先,闭包初始化 | XML优先,声明式布局 |
布局系统 | Auto Layout,约束驱动 | 多种布局,ConstraintLayout推荐 |
样式管理 | 代码设置,Appearance代理 | XML样式,主题系统完善 |
数据绑定 | 手动绑定,Combine框架 | LiveData观察者模式 |
导航模式 | Navigation Controller栈 | Intent系统,Fragment管理 |
开发工具 | Interface Builder可选 | Layout Editor主流 |
预览功能 | SwiftUI Preview | Layout Preview |
热重载 | 有限支持 | Instant Run/Apply Changes |
学习建议
从Android转iOS:
- 🎯 重点掌握Auto Layout约束系统
- 🎯 熟悉闭包初始化UI组件的模式
- 🎯 理解Target-Action事件处理机制
- 🎯 学习Navigation Controller的栈式导航
从iOS转Android:
- 🎯 掌握XML布局和ConstraintLayout
- 🎯 熟悉样式和主题系统
- 🎯 理解Activity和Fragment生命周期
- 🎯 学习Intent系统和数据传递
3. 生命周期方法
iOS:
swift
override func viewDidLoad() {
super.viewDidLoad() // 相当于Android的onCreate()
setupUI() // 初始化UI
setupConstraints() // 设置约束
setupActions() // 绑定事件
}
Android:
kotlin
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) // 设置布局
setupUI() // 初始化UI
setupListeners() // 绑定事件
}
4. 布局系统:Auto Layout vs ConstraintLayout
iOS Auto Layout (代码方式):
swift
NSLayoutConstraint.activate([
// 水平居中
helloLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
// 垂直位置
helloLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -60),
// 边距约束
helloLabel.leadingAnchor.constraint(greaterThanOrEqualTo: view.leadingAnchor, constant: 20)
])
Android ConstraintLayout (XML方式):
xml
<TextView
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
5. 事件处理:Target-Action vs Listener
iOS Target-Action 模式:
swift
// 绑定事件
tapButton.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
// 事件处理方法
@objc private func buttonTapped() {
tapCount += 1
// 处理点击事件
}
Android Listener 模式:
kotlin
// 绑定事件
tapButton.setOnClickListener {
tapCount++
// 处理点击事件
}
6. 动画系统
iOS UIView 动画:
swift
UIView.animate(withDuration: 0.3, animations: {
self.tapButton.transform = CGAffineTransform(scaleX: 0.95, y: 0.95)
}) { _ in
UIView.animate(withDuration: 0.3) {
self.tapButton.transform = .identity
}
}
Android 属性动画:
kotlin
val scaleDown = ObjectAnimator.ofFloat(tapButton, "scaleX", 1f, 0.95f)
val scaleUp = ObjectAnimator.ofFloat(tapButton, "scaleX", 0.95f, 1f)
AnimatorSet().apply {
play(scaleDown).before(scaleUp)
duration = 300
start()
}
AppDelegate 与 SceneDelegate
历史背景
AppDelegate 是 iOS 应用的传统架构核心,从 iOS 2.0 开始就存在。而 SceneDelegate 是 iOS 13.0 引入的新概念,用于支持多窗口应用。
核心区别
职责范围
AppDelegate(应用级别):
- 管理整个应用的生命周期
- 处理应用启动、终止、后台等全局事件
- 管理应用级别的配置和服务
- 在 iOS 13 之前,也负责窗口管理
SceneDelegate(场景级别):
- 管理单个窗口/场景的生命周期
- 处理窗口的创建、激活、失活等事件
- 支持多窗口应用(iPad 分屏、Mac Catalyst)
- 只在 iOS 13+ 中可用
代码对比分析
AppDelegate.swift 中的关键代码:
swift
// iOS 12 及以下的窗口管理方式
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
return true
}
// iOS 13+ 的场景配置方法
@available(iOS 13.0, *)
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
SceneDelegate.swift 中的关键代码:
swift
// iOS 13+ 的窗口管理方式
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
}
执行流程
iOS 13+ 设备上的执行顺序:
- AppDelegate.application(_:didFinishLaunchingWithOptions:) - 应用启动
- AppDelegate.application(_:configurationForConnecting:options:) - 配置场景
- SceneDelegate.scene(_:willConnectTo:options:) - 创建窗口
- SceneDelegate.sceneDidBecomeActive(_😃 - 场景激活
iOS 12 及以下设备:
- AppDelegate.application(_:didFinishLaunchingWithOptions:) - 应用启动并创建窗口
- 直接使用 AppDelegate 管理窗口生命周期
AppDelegate 和 SceneDelegate 的连接机制
重要发现:AppDelegate 中没有直接引用 SceneDelegate
很多开发者会疑惑:AppDelegate 和 SceneDelegate 之间是如何连接的? 实际上,它们之间是"松耦合"的关系,通过系统配置文件连接,而不是直接的代码引用。
连接机制详解
1. Info.plist 配置文件是关键
xml
<key>UISceneDelegate</key>
<dict>
<key>UIWindowSceneSessionRoleApplication</key>
<array>
<dict>
<key>UISceneConfigurationName</key>
<string>Default Configuration</string>
<key>UISceneDelegateClassName</key>
<string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
</dict>
</array>
</dict>
2. AppDelegate 的间接连接
swift
// AppDelegate.swift 中的关键方法
@available(iOS 13.0, *)
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// 返回配置名称,对应 Info.plist 中的 UISceneConfigurationName
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
3. 完整的连接流程
应用启动
↓
AppDelegate.didFinishLaunchingWithOptions
↓
系统请求场景配置
↓
AppDelegate.configurationForConnecting 返回 "Default Configuration"
↓
系统查找 Info.plist 中名为 "Default Configuration" 的配置
↓
找到对应的 UISceneDelegateClassName: SceneDelegate
↓
系统自动实例化 SceneDelegate 类
↓
SceneDelegate.scene(_:willConnectTo:) 被调用
↓
创建和管理窗口
关键理解点
松耦合设计的优势:
- 灵活性:可以通过修改 Info.plist 更换不同的 SceneDelegate 类
- 可配置性:支持多种场景配置,适应不同的应用需求
- 向后兼容:iOS 12 及以下版本会忽略 Scene 相关配置
- 系统管理:由系统负责实例化和生命周期管理,减少样板代码
为什么不直接引用?
- 支持多场景应用(一个应用可以有多个不同的 SceneDelegate)
- 允许动态配置场景类型
- 保持架构的清晰分离
是否有必要都存在?
建议保留两者的情况:
-
需要支持 iOS 12 及以下版本
- AppDelegate 处理旧版本的窗口管理
- SceneDelegate 处理新版本的场景管理
-
计划支持多窗口功能
- iPad 分屏应用
- Mac Catalyst 应用
-
渐进式迁移
- 保持向后兼容性
- 逐步采用新架构
可以简化的情况:
-
只支持 iOS 13+
- 可以移除 AppDelegate 中的窗口管理代码
- 专注使用 SceneDelegate
-
简单的单窗口应用
- 可以在 Info.plist 中移除 Scene 配置
- 只使用 AppDelegate(传统方式)
开发工具与快捷键
Xcode 常用快捷键
导航和搜索
功能 | Xcode | Android Studio (macOS) |
---|---|---|
全局搜索 | ⌘ + Shift + F |
⌘ + Shift + F |
文件搜索 | ⌘ + Shift + O |
⌘ + Shift + O |
类/符号搜索 | ⌘ + Shift + O |
⌘ + O |
快速打开 | ⌘ + T |
⌘ + Shift + A |
代码编辑
功能 | Xcode | Android Studio (macOS) |
---|---|---|
代码补全 | Ctrl + Space |
Ctrl + Space |
格式化代码 | Ctrl + I |
⌘ + Alt + L |
注释/取消注释 | ⌘ + / |
⌘ + / |
重命名 | ⌘ + Ctrl + E |
Shift + F6 |
跳转和导航
功能 | Xcode | Android Studio (macOS) |
---|---|---|
跳转到定义 | ⌘ + Ctrl + J 或 ⌘ + 点击 |
⌘ + B 或 ⌘ + 点击 |
返回上一位置 | ⌘ + Ctrl + ← |
⌘ + Alt + ← |
前进下一位置 | ⌘ + Ctrl + → |
⌘ + Alt + → |
查找用法 | ⌘ + Shift + F |
Alt + F7 |
构建和运行
功能 | Xcode | Android Studio (macOS) |
---|---|---|
构建项目 | ⌘ + B |
⌘ + F9 |
运行应用 | ⌘ + R |
Ctrl + R |
停止运行 | ⌘ + . |
⌘ + F2 |
清理构建 | ⌘ + Shift + K |
无直接对应 |
自定义快捷键
在 Xcode 中自定义快捷键:
- 打开 Xcode → Preferences → Key Bindings
- 搜索要修改的功能
- 双击快捷键列进行修改
- 可以导出/导入快捷键配置文件
总结与建议
关键语法特点总结
-
Swift vs Kotlin/Java:
- Swift 更函数式,大量使用闭包
- 可选类型 (
?
) vs Nullable types - 属性观察器 vs 传统getter/setter
-
布局哲学:
- iOS:代码布局为主,约束系统
- Android:XML布局为主,声明式
-
生命周期:
- iOS:
viewDidLoad
→viewWillAppear
→viewDidAppear
- Android:
onCreate
→onStart
→onResume
- iOS:
-
事件处理:
- iOS:Target-Action 模式,需要
@objc
标记 - Android:Listener 接口,Lambda 表达式
- iOS:Target-Action 模式,需要
学习建议
- 从基础开始:理解 MVC 架构和生命周期
- 实践为主:多写代码,熟悉 Swift 语法
- 对比学习:利用已有的 Android 经验类比学习
- 关注差异:重点理解两个平台的设计哲学差异
- 工具熟练:掌握 Xcode 的使用和调试技巧
下一步学习方向
- UI 进阶:学习 UIKit 更多组件和布局技巧
- 数据管理:Core Data、UserDefaults、网络请求
- 架构模式:MVVM、Coordinator 等现代架构
- SwiftUI:苹果的声明式 UI 框架
- 性能优化:内存管理、性能调试工具
UIKit 框架详解
什么是 UIKit?
UIKit 是苹果公司为 iOS 和 tvOS 开发提供的核心用户界面框架。它是构建 iOS 应用程序用户界面的基础框架,提供了创建和管理应用程序界面所需的所有基本组件和功能。
UIKit 的核心定位
swift
import UIKit // 导入UIKit框架
// UIKit是iOS应用开发的核心框架
// 几乎所有的UI相关类都来自UIKit
class ViewController: UIViewController { // 继承自UIKit的UIViewController
let label = UILabel() // UIKit的标签组件
let button = UIButton() // UIKit的按钮组件
let imageView = UIImageView() // UIKit的图片组件
}
UIKit 框架的组成部分
1. 视图和控件 (Views and Controls)
基础视图类:
UIView
- 所有视图的基类UILabel
- 文本显示UIButton
- 按钮控件UIImageView
- 图片显示UITextField
- 文本输入框UITextView
- 多行文本编辑
容器视图:
UIScrollView
- 滚动视图UITableView
- 表格视图UICollectionView
- 集合视图UIStackView
- 堆栈视图
控制组件:
UISwitch
- 开关UISlider
- 滑动条UISegmentedControl
- 分段控制UIProgressView
- 进度条UIStepper
- 步进器
2. 视图控制器 (View Controllers)
swift
// 视图控制器是UIKit的核心概念
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// 视图加载完成后的初始化
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// 视图即将出现时的处理
}
}
// 特殊的视图控制器
class MyTableViewController: UITableViewController {
// 专门用于管理表格视图
}
class MyNavigationController: UINavigationController {
// 用于导航管理
}
3. 布局系统 (Layout System)
Auto Layout:
swift
// UIKit的约束布局系统
NSLayoutConstraint.activate([
label.centerXAnchor.constraint(equalTo: view.centerXAnchor),
label.centerYAnchor.constraint(equalTo: view.centerYAnchor),
label.widthAnchor.constraint(equalToConstant: 200),
label.heightAnchor.constraint(equalToConstant: 50)
])
Frame-based Layout:
swift
// 传统的坐标布局方式
label.frame = CGRect(x: 100, y: 200, width: 200, height: 50)
4. 事件处理系统 (Event Handling)
Target-Action 模式:
swift
// UIKit的事件处理机制
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
@objc func buttonTapped() {
print("按钮被点击了")
}
手势识别:
swift
// 手势识别器
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap))
view.addGestureRecognizer(tapGesture)
5. 动画系统 (Animation System)
swift
// UIKit提供的动画API
UIView.animate(withDuration: 0.3) {
self.view.backgroundColor = .red
self.label.alpha = 0.5
}
// 更复杂的动画
UIView.animate(withDuration: 0.5,
delay: 0.1,
usingSpringWithDamping: 0.8,
initialSpringVelocity: 0.2,
options: .curveEaseInOut) {
self.button.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
}
UIKit 与其他框架的关系
UIKit vs SwiftUI
特性 | UIKit | SwiftUI |
---|---|---|
发布时间 | 2008年 (iOS 2.0) | 2019年 (iOS 13.0) |
编程范式 | 命令式编程 | 声明式编程 |
布局方式 | Auto Layout + Frame | 声明式布局 |
状态管理 | 手动管理 | 自动响应式 |
学习曲线 | 较陡峭 | 相对平缓 |
兼容性 | iOS 2.0+ | iOS 13.0+ |
swift
// UIKit 方式
class UIKitViewController: UIViewController {
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
label.text = "Hello UIKit"
}
}
// SwiftUI 方式
struct SwiftUIView: View {
var body: some View {
Text("Hello SwiftUI")
}
}
UIKit 与 Foundation 的关系
swift
import Foundation // 基础框架
import UIKit // UI框架,依赖Foundation
// Foundation提供基础数据类型
let string: String = "Hello" // Foundation
let array: Array<Int> = [1, 2, 3] // Foundation
let url: URL = URL(string: "https://apple.com")! // Foundation
// UIKit提供UI组件
let label: UILabel = UILabel() // UIKit
let button: UIButton = UIButton() // UIKit
let view: UIView = UIView() // UIKit
UIKit 的核心设计模式
1. MVC (Model-View-Controller)
swift
// Model - 数据模型
struct User {
let name: String
let age: Int
}
// View - 视图 (UIKit组件)
class UserView: UIView {
let nameLabel = UILabel()
let ageLabel = UILabel()
}
// Controller - 控制器
class UserViewController: UIViewController {
let userView = UserView()
var user: User?
override func viewDidLoad() {
super.viewDidLoad()
updateUI()
}
func updateUI() {
userView.nameLabel.text = user?.name
userView.ageLabel.text = "\(user?.age ?? 0)"
}
}
2. Delegate 模式
swift
// UIKit大量使用委托模式
class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
return cell
}
}
UIKit 的优势和特点
优势:
- 成熟稳定 - 经过15年的发展和优化
- 功能完整 - 提供了构建复杂应用所需的所有组件
- 性能优秀 - 经过高度优化,运行效率高
- 兼容性好 - 支持从iOS 2.0到最新版本
- 生态丰富 - 大量第三方库和资源
- 精确控制 - 可以精确控制每个像素和动画
特点:
- 命令式编程 - 需要明确告诉系统如何做
- 手动内存管理 - 需要注意循环引用等问题
- 生命周期管理 - 需要理解视图控制器的生命周期
- 事件驱动 - 基于事件和回调的编程模式
学习 UIKit 的建议
学习路径:
- 基础概念 - 理解视图、视图控制器、生命周期
- 基本组件 - 掌握常用的UI组件使用
- 布局系统 - 学习Auto Layout约束布局
- 事件处理 - 理解Target-Action和手势识别
- 高级特性 - 动画、自定义控件、性能优化
实践建议:
- 从简单开始 - 先做Hello World类型的应用
- 多写代码 - UIKit需要大量实践才能熟练
- 阅读文档 - 苹果官方文档是最好的学习资源
- 参考示例 - 学习优秀的开源项目代码
- 调试技巧 - 掌握Xcode的调试和性能分析工具
总结
UIKit 是 iOS 开发的核心框架,它提供了构建用户界面所需的所有基础组件和功能。虽然苹果推出了 SwiftUI 作为新的 UI 框架,但 UIKit 仍然是 iOS 开发的重要基础,特别是在需要兼容旧版本 iOS 或需要精确控制 UI 的场景下。
对于初学者来说,理解 UIKit 的核心概念(视图、视图控制器、生命周期、事件处理)是掌握 iOS 开发的关键。通过实际项目练习,逐步掌握 UIKit 的各种组件和特性,是成为优秀 iOS 开发者的必经之路。