@main struct WorldLandMarkApp: App { var body: some Scene { WindowGroup { ContentView() } } }
public protocol App { associatedtype Body : Scene @SceneBuilder @MainActor var body: Self.Body { get } @MainActor init() }
struct LandmarkList: View { var body: some View { Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/) } }
public protocol View { associatedtype Body : View @ViewBuilder @MainActor var body: Self.Body { get } }
import SwiftUI struct ContentView: View { var body: some View { Text("Hello, world!") .padding() } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
struct LandmarkList_Previews: PreviewProvider { struct DeviceType: Identifiable { var id = UUID() var name: String } static var previews: some View { //使用ForEach展示多个设备 ForEach([DeviceType(name: "iPhone 12"),DeviceType(name: "iPhone 13")]){ deviceItem in LandmarkList().previewDevice(PreviewDevice(rawValue: deviceItem.name)) .previewDisplayName(deviceItem.name) } } }
@State private var isOpen struct LandmarkList: View { @State private var isOpen: Bool = false //@ObservedObject: 全局环境变量绑定 @ObservedObject var userData: UserData = UserData() }
import SwiftUI import Combine class UserData: ObservableObject { @Published var userLandmarks:[Landmark] = landmarks }
struct LandmarkDetail: View { var landmark: Landmark @ObservedObject var userData: UserData var userLandmarkIndex: Int { userData.userLandmarks.firstIndex(where: {$0.id == landmark.id})! } var body: some View { Button(action: { self.userData.userLandmarks[self.userLandmarkIndex] .isFeatured.toggle() }){ if landmark.isFeatured { Image("icon_rcxinhua_selected") .resizable().frame(width: 20, height: 20, alignment: .center) } else { Image("icon_rcxinhua_defaultselected") .resizable().frame(width: 20, height: 20, alignment: .center) } } } }
struct MapView: UIViewRepresentable { let view: UIView = UIView() func makeUIView(context: Context) -> some UIView { return view } func updateUIView(_ uiView: UIViewType, context: Context) { view.backgroundColor = .red } }
UIHostingController(rootView: ContentView())
struct Landmark: Identifiable { var id = UUID() let name: String }