预览示例
在SwiftUI预览的过程中,通常需要配置预览的对象:
#Preview {
EditView(location: Location(id: UUID, name: String, description: String, latitude: Double, longitude: Double), name: String, description: String)
}
在Location中存在多个属性:
struct Location: Codable, Equatable, Identifiable {
static func == (lhs: Location, rhs: Location) -> Bool {
return lhs.id == rhs.id
}
let id: UUID
var name: String
var description: String
var latitude: Double
var longitude: Double
var coordinate: CLLocationCoordinate2D {
CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
}
}
如果有多个视图预览中涉及Location结构,那么一般情况下,都需要在预览中定义一个预览实例:
#Preview {
EditView(location: Location(
id: UUID(),
name: "Buckingham Palace",
description: "Lit by over 40,000 lightbulbs.",
latitude: 51.501,
longitude: -0.141
),name: "Buckingham Palace",description: "Lit by over 40,000 lightbulbs.")
}
示例数据
为了方便测试或预览,可以在结构体内定义一个静态属性,用于提供一个示例实例。
struct Location: Codable, Equatable, Identifiable {
...
static let example = Location(
id: UUID(),
name: "Buckingham Palace",
description: "Lit by over 40,000 lightbulbs.",
latitude: 51.501,
longitude: -0.141
)
}
example 是一个 Location 类型的静态属性。
它是通过调用 Location 的初始化器来创建的一个具体实例。
用途:为开发提供一个示例 Location 实例,通常用于调试、测试、或 SwiftUI 预览中。
#Preview {
EditView(location: Location.example,name: Location.example.name,description: Location.example.description)
}
为什么要定义一个 example 属性?
1、简化调试和预览
在调试或 SwiftUI 预览中需要传递一个 Location 对象时,可以直接使用 Location.example,而无需手动创建新实例。
例如:
#Preview {
EditView(location: Location.example)
}
这样避免重复创建实例,并提高代码的可读性。
2、避免重复代码
如果你在多个地方都需要一个 Location 对象作为样本数据,通过 example 可以避免多次重复写类似的初始化代码。
3、演示用例
在学习或文档中使用 example 来展示结构体的典型用法。
访问 example 属性
因为 example 是静态属性,可以通过 Location 类型直接访问它:
let sampleLocation = Location.example
如果它是实例属性(没有 static),则需要创建一个 Location 实例才能访问:
let locationInstance = Location(/* initialization values */)
let example = locationInstance.example // 如果没有 static,就得这样用
总结
example 是一个便捷的示例数据静态属性,用于开发、调试、和测试。
完整代码
EditView视图
import Foundation
import SwiftUI
struct EditView: View {
@Environment(\.dismiss) var dismiss
var location: Location
@State private var name: String
@State private var description: String
init(location: Location,name: String, description: String) {
self.location = location
_name = State(initialValue: location.name)
_description = State(initialValue: location.description)
}
var body: some View {
NavigationStack {
Form {
Section {
TextField("Place name", text: $name)
TextField("Description", text: $description)
}
}
.navigationTitle("Place details")
.toolbar {
Button("Save") {
dismiss()
}
}
}
}
}
#Preview {
EditView(location: Location.example,name: Location.example.name,description: Location.example.description)
}
Location结构
import Foundation
import MapKit
struct Location: Codable, Equatable, Identifiable {
static func == (lhs: Location, rhs: Location) -> Bool {
return lhs.id == rhs.id
}
let id: UUID
var name: String
var description: String
var latitude: Double
var longitude: Double
var coordinate: CLLocationCoordinate2D {
CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
}
static let example = Location(
id: UUID(),
name: "Buckingham Palace",
description: "Lit by over 40,000 lightbulbs.",
latitude: 51.501,
longitude: -0.141
)
}