Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: 王 巍

Rating No ratings yet

No description

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 A hands-on guide for iOS developers who want to master SwiftUI's declarative UI paradigm and Combine's reactive programming, building real apps from a calculator to a Pokémon viewer while learning state management and asynchronous data flow. 【Book Arc】 - **Opening (~0%–5%)**: Introduces SwiftUI as Apple's declarative replacement for imperative UIKit, contrasting "how to do" with "what to do." Starts building a calculator app UI using SwiftUI's DSL, covering View modifiers, Text styling, and layout basics. - **Early (~5%–21%)**: Deepens the calculator project with layout refinements (Spacer, frame alignment), then shifts to state management. Explains @State, @Binding, and property wrappers for passing data through view hierarchies, plus a pure-function state machine for calculator logic and a history feature using .sheet. - **Early (~21%–32%)**: Transitions to a larger Pokémon app example. Covers list creation with ScrollView, LazyVStack, and ForEach, building custom cells with nested stacks, SF Symbols, and expandable rows. Introduces ViewModels (PokemonViewModel, AbilityViewModel) and local JSON loading. - **Middle (~32%–42%)**: Continues the Pokémon app with detail panels, blur effects, and a Settings model using ObservableObject and @Published. Then pivots to Combine fundamentals: Publisher, Subscriber, and Operator roles, with sink and assign as subscription methods bridging reactive and imperative worlds. - **Middle (~42%–58%)**: Focuses on practical Combine usage in Playgrounds: common Publishers (Just, Future, PassthroughSubject), operators like reduce, scan, flatMap, zip, and timing controls (delay, debounce). Covers bridging async APIs with Future and Subject, and @Published's automatic Publisher generation. - **Late (~58%–end)**: Explores subscription and binding patterns, including pitfalls like duplicate network requests from multiple assign calls. Discusses Redux-like architecture adapted for SwiftUI, using a Store with Actions and Reducers, while acknowledging SwiftUI's Binding as a complement to strict Redux flow. 【Key Takeaways】 - **Declarative UI with View modifiers** (Early): SwiftUI builds interfaces by describing results, not steps. Modifiers like .font, .padding, and .background wrap views to create new versions, enabling reusable components like CalculatorButton. Use predefined fonts for Dynamic Type support. - **@State and @Binding for state flow** (Early): @State triggers UI refresh on value change but copies values across view layers, breaking updates. @Binding converts value semantics to reference semantics, letting deeply nested views (buttons in rows in pads) mutate the top-level model. - **Property wrappers generalize getter/setter logic** (Early): Custom wrappers like @Converter encapsulate repeated transformations (currency conversion, UserDefaults access). They expose wrappedValue and projectedValue ($ prefix) for read/write and derived data. - **ObservableObject and @Published drive SwiftUI models** (Early): Classes conforming to ObservableObject with @Published properties automatically notify views of changes. ViewModels like PokemonViewModel combine multiple data models to serve specific UI needs, keeping views simple. - **Combine's three core roles** (Middle): Publisher emits events, Subscriber consumes them, and Operator transforms streams. sink bridges to closure-based imperative code, while assign binds values directly to object properties via key paths, enabling pure declarative updates. - **Operators manage values and timing** (Middle): map and scan transform event values (e.g., reduce to a sum), while receive(on:), delay, and debounce handle scheduling and throttling. These are Publisher extensions returning new Publishers, central to Combine's composability. - **Future and Subject bridge async code** (Middle): Future wraps single-result async tasks like network requests (success or error only). Subject (e.g., PassthroughSubject) handles multiple events—keyboard notifications, timers—by sending values into a subscribable stream. - **Avoid duplicate subscriptions** (Late): Assigning one Publisher's output to multiple properties via separate assign calls can trigger multiple executions (e.g., two network requests). Share or restructure the pipeline to ensure single side effects. 【Reading Tips】 - **Skim the calculator UI chapters (0–16%)** if you're already comfortable with SwiftUI basics; focus instead on the state machine design and @Binding discussion, which are the conceptual core. - **Deep-read the Combine chapters (37–58%)** with the provided Playground open. Run each example (reduce, flatMap, debounce) and modify inputs to internalize operator behavior—this is where the book's value peaks. - **Treat the Pokémon app (21–37%) as a reference project**: skim cell layouts and Settings UI, but study how ViewModels are structured and how local JSON is loaded—patterns you'll reuse in real apps. - **Watch for the Redux discussion near the end**: it's a condensed architecture lesson. If you're not building large apps, skim it; if you are, take notes on how Actions/Reducers adapt to SwiftUI's Binding model. - **Don't skip the exercises** (marked with hints like "try using ZStack"): they force you to reimplement patterns (e.g., redrawing buttons, adding debounce to search) and solidify understanding better than re-reading. 【Coverage Limits】 Excerpts do not cover the book's final chapters on networking with URLSession, Swift Package Manager integration, or image downloading in detail—these are mentioned as upcoming topics but not fully demonstrated in the sampled material.
Excerpt 1
字体,使用预定义的字体可以让我们不需 要额外的工作就能适配 Dynamic Type特性。除非有特别的字体尺寸偏好,否 则对于需要跟随用户字体大小设置进行缩放的内容,最好都应该按照 HIG的 定义选择预定义的字号。 2. 使用 foregroundColor设定文本的颜色。除了像是 Color.white或者 C...
View in text
Excerpt 2
制台输出记录的每一步操作: !" ContentView var body: some View { Spacer() Button("操作履历: \(model.history.count)") { self.editingHistory = true }.sheet(isPresented: self.$ed...
View in text
Excerpt 3
ewModel对应的,我们暂时用 sample(pokemonID:)来避免意外地获取到不正确的技能。不过要记住,在本 地我们暂时只有两个技能的 JSON,因此任何尝试加载不存在的技能的行为, 会造成崩溃。我们会在后面的章节实际使用 API返回的数据,来修正这个问题。 2. 为了使 Xcode预览继续工作,需要传...
View in text
Excerpt 4
。在真实世界里的情况,往往会有多个 Publisher协同工作。每多一个事件流,就会让我们的思考维度加一,如果这些事件 流之间还有状态共享关系的话,它们之间的关系会变得非常复杂。通过将多个事件 流合并和降维,我们可以明确地保持只处理单个事件流,而忽略掉不必要的实现细 节,这与声明式编程的思想不谋而合,也是这套方式...
View in text
Excerpt 5
icker( selection: settingsBinding.accountBehavior, label: Text("")) { ForEach( AppState.Settings.AccountBehavior.allCases, id: \.self) { Text($0.text) } } 这样...
View in text
Excerpt 6
fisher的框架来解决这个问题。 Kingfisher是一个图片下载和缓存的框架,而且最近它也支持了 SwiftUI。我们只需 要把 URL传递进去,它就能帮助我们处理一系列异步图片加载的任务:包括尝试从 缓存中寻找图片,如果没有找到则从 URL下载图片,并将它缓存起来供日后使用。 这可以让我们的代码变得非常简...
View in text
Excerpt 7
修改: struct MainTab: View { var body: some View { !" 1 练习 1.更改 SafariView的展示方式 在示例 app中,我们实现了使用 NavigationLink来把 SafariView推入到界面中。请 你尝试修改为使用sheet方法来把这个网页以moda...
View in text
Excerpt 8
ack可以同时接受水平和竖直两个方向上的对齐。这也是使用多个 alignmentGuide进行不同方向对齐的先决条件。 你可以使用源码文件夹中 “PokeMaster-Finished”里的项目作为本题练习的起始。
View in text
Tags
AI categories
Programming Languageiosswiftui
Publish Year: 2020
Language: Chinese
Pages: 412
File Format: PDF
File Size: 7.8 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…