Post

Replies

Boosts

Views

Activity

Environment object become nil
I have a UIKit project, and I added a SwiftUI page into the project. And I pushed the SwiftUI page with the following code.    @IBAction func push(_ sender: Any) {      let viewController = UIHostingController(rootView: OrangeConfigListView().environmentObject(Store()))      navigationController?.pushViewController(viewController, animated: true)   } And here's the models. struct Group: Codable, Identifiable { 		var id:Int { 				name.hash 		} 		var name: String 		var configs: [Config] } struct Config: Codable, Identifiable { 		var key: String 		var value: String 		var id: Int { 				key.hash 		} 		var mocked = false 		var mockedValue: String? 		 		enum CodingKeys: String, CodingKey { 				case key 				case value 		} } final class Store: ObservableObject { 		@Published var groups: [Group] 		 		init() { 				/* load groups */ 		} 		 		func mock(group: Group, config: Config, mockedValue: String) { 				let groupIndex = groups.firstIndex(matching: group) 				let configIndex = group.configs.firstIndex(matching: config) 				groups[groupIndex].configs[configIndex].mocked = true 				groups[groupIndex].configs[configIndex].mockedValue = mockedValue 		} 		 		func unmock(group: Group, config: Config) { 				let groupIndex = groups.firstIndex(matching: group) 				let configIndex = group.configs.firstIndex(matching: config) 				groups[groupIndex].configs[configIndex].mocked = false 				groups[groupIndex].configs[configIndex].mockedValue = nil 		} } And here's the view code. struct OrangeConfigCell: View {   @State var text: String   var config: OrangeConfig   var mockAction: (String) -> Void   var body: some View {     VStack {       HStack {         Text(config.key)           .font(.subheadline)         Spacer()         Button(config.mocked ? "Unmock" : "Mock") {           self.mockAction(text)         }         .foregroundColor(.blue)       }       TextView(text: $text, borderColor: config.mocked ? colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1) : colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1))         .frame(height: 200)     }     .buttonStyle(PlainButtonStyle())   } } But when I triggered 'mockAction', I something wired just happened, the 'store' become nil! How would this happen ?
1
0
1.4k
Oct ’21
Environment object become nil
I have a UIKit project, and I added a SwiftUI page into the project. And I pushed the SwiftUI page with the following code.    @IBAction func push(_ sender: Any) {      let viewController = UIHostingController(rootView: OrangeConfigListView().environmentObject(Store()))      navigationController?.pushViewController(viewController, animated: true)   } And here's the models. struct Group: Codable, Identifiable { 		var id:Int { 				name.hash 		} 		var name: String 		var configs: [Config] } struct Config: Codable, Identifiable { 		var key: String 		var value: String 		var id: Int { 				key.hash 		} 		var mocked = false 		var mockedValue: String? 		 		enum CodingKeys: String, CodingKey { 				case key 				case value 		} } final class Store: ObservableObject { 		@Published var groups: [Group] 		 		init() { 				/* load groups */ 		} 		 		func mock(group: Group, config: Config, mockedValue: String) { 				let groupIndex = groups.firstIndex(matching: group) 				let configIndex = group.configs.firstIndex(matching: config) 				groups[groupIndex].configs[configIndex].mocked = true 				groups[groupIndex].configs[configIndex].mockedValue = mockedValue 		} 		 		func unmock(group: Group, config: Config) { 				let groupIndex = groups.firstIndex(matching: group) 				let configIndex = group.configs.firstIndex(matching: config) 				groups[groupIndex].configs[configIndex].mocked = false 				groups[groupIndex].configs[configIndex].mockedValue = nil 		} } And here's the view code. struct OrangeConfigCell: View {   @State var text: String   var config: OrangeConfig   var mockAction: (String) -> Void   var body: some View {     VStack {       HStack {         Text(config.key)           .font(.subheadline)         Spacer()         Button(config.mocked ? "Unmock" : "Mock") {           self.mockAction(text)         }         .foregroundColor(.blue)       }       TextView(text: $text, borderColor: config.mocked ? colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1) : colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1))         .frame(height: 200)     }     .buttonStyle(PlainButtonStyle())   } } But when I triggered 'mockAction', I something wired just happened, the 'store' become nil! How would this happen ?
Replies
1
Boosts
0
Views
1.4k
Activity
Oct ’21
Terminal in Xcode
It's really boring to switch between terminal and Xcode. Xcode is about to release 12 version, why there's still no terminal in Xcode
Replies
4
Boosts
9
Views
28k
Activity
Sep ’21