Post

Replies

Boosts

Views

Activity

Reply to Fetch JSON Data not working
I tested in playground and got nothing. Problem is probably your line 41. I modified like this and seemed to work much better when appending after completion: var users : [User] = [] let can = URLSession.shared.dataTaskPublisher(for: uUrl) 		.map { $0.data } 		.decode(type: [User].self, decoder: JSONDecoder()) 		.replaceError(with: []) 		.eraseToAnyPublisher() 		.receive(on: DispatchQueue.main) 		.sink(receiveCompletion: { print ("Received completion: \($0)."); print(users.count, users[1]) }, 					receiveValue: { user in print ("Received user: \(user).") ; users.append(contentsOf: user) 					}) print gives: 56 User(state: "AL") Note: see this https://developer.apple.com/documentation/foundation/urlsession/processing_url_session_data_task_results_with_combine
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20
Reply to Fetch JSON Data not working
So, your init would be 		init() { 				var receivedUsers : [User] = [] 				self.can = URLSession.shared.dataTaskPublisher(for: url) 						.map { $0.data } 						.decode(type: [User].self, decoder: JSONDecoder()) 						.replaceError(with: []) 						.eraseToAnyPublisher() 						.receive(on: DispatchQueue.main) 						.sink(receiveCompletion: { _ in users = receivedUsers }, 										receiveValue: { user in receivedUsers.append(contentsOf: user) 					}) 		} On your posts: when you reply, please tell which proposal you replied to don't just say "it doesn't work". Take time, as those who answer to help you, to explain what you exactly tried, what you get,
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20
Reply to Xcode requirements
SSD would speed up, but not to the level you seem to expect. And HD slows down mainly if you have to swap from RAM to Disk. So at the end, we fall back on RAM capacity. My advice: if you can have access to an Apple store, ask them what are the options and cost to increase your Mac 21" at least to 16 GB. And if not too expensive, go for 32 GB.
Dec ’20
Reply to Looping views SwiftUI
Would this do it ? import SwiftUI struct ContentView: View { &#9;&#9;@State var view1 = false &#9;&#9;@State var view2 = false &#9;&#9;@State var view3 = false &#9;&#9;@State var view4 = false &#9;&#9;@State var views = [false, false, false, false] &#9;&#9; &#9;&#9;private var gridItemLayout = [GridItem(.flexible()), GridItem(.flexible())] &#9;&#9; &#9;&#9;private var viewTexts = ["View1", "View2", "View3", "View4"] &#9;&#9; &#9;&#9;var textSize: CGFloat { &#9;&#9;&#9;&#9;if UIDevice.current.userInterfaceIdiom == .pad { &#9;&#9;&#9;&#9;&#9;&#9;return 48 &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;return 23 &#9;&#9;} &#9;&#9; &#9;&#9;var title: CGFloat { &#9;&#9;&#9;&#9;if UIDevice.current.userInterfaceIdiom == .pad { &#9;&#9;&#9;&#9;&#9;&#9;return 60 &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;return 34 &#9;&#9;} &#9;&#9; &#9;&#9;var height: CGFloat { &#9;&#9;&#9;&#9;if UIDevice.current.userInterfaceIdiom == .pad { &#9;&#9;&#9;&#9;&#9;&#9;return 0.15 &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;return 0.15 &#9;&#9;} &#9;&#9; &#9;&#9;var weight: CGFloat { &#9;&#9;&#9;&#9;if UIDevice.current.userInterfaceIdiom == .pad { &#9;&#9;&#9;&#9;&#9;&#9;return 0.44 &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;return 0.43 &#9;&#9;} &#9;&#9; &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;NavigationView{ &#9;&#9;&#9;&#9;&#9;&#9;GeometryReader { geometry in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;ScrollView(.vertical, showsIndicators: true) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;LazyVGrid(columns: gridItemLayout, spacing: 18) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Group { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;ForEach(0..<views.count) { i in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text(viewTexts[i]) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.foregroundColor(.black) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.frame(width: geometry.size.width * weight, height: geometry.size.height * height) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.background(Color.white) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.onTapGesture { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;views[i] = true &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.sheet(isPresented: $views[i]) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;switch i { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;case 0: View1() &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;case 1: View2() &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;case 2: View3() &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;case 3: View4() &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;default: View1() &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;}.padding() &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;.navigationTitle("Title") &#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;} &#9;&#9;} &#9;&#9; } struct View1: View { &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;Text("View1") &#9;&#9;} } struct View2: View { &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;Text("View2") &#9;&#9;} } struct View3: View { &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;Text("View3") &#9;&#9;} } struct View4: View { &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;Text("View4") &#9;&#9;} } You can go one step further Declare let Views : [AnyView] = [AnyView(View1()), AnyView(View2()), AnyView(View3()), AnyView(View4())] and replace the switch lines 57 to 63 by: .sheet(isPresented: $views[i]) { &#9;&#9; Views[i] }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20
Reply to Looping views SwiftUI
So, the code is now refactored: import SwiftUI struct ContentView: View { &#9;&#9;@State var views = [false, false, false, false] &#9;&#9; &#9;&#9;let Views : [AnyView] = [AnyView(View1()), AnyView(View2()), AnyView(View3()), AnyView(View4())] &#9;&#9;private var gridItemLayout = [GridItem(.flexible()), GridItem(.flexible())] &#9;&#9;private var viewTexts = ["View1", "View2", "View3", "View4"] &#9;&#9; &#9;&#9;var textSize: CGFloat { &#9;&#9;&#9;&#9;if UIDevice.current.userInterfaceIdiom == .pad { &#9;&#9;&#9;&#9;&#9;&#9;return 48 &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;return 23 &#9;&#9;} &#9;&#9; &#9;&#9;var title: CGFloat { &#9;&#9;&#9;&#9;if UIDevice.current.userInterfaceIdiom == .pad { &#9;&#9;&#9;&#9;&#9;&#9;return 60 &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;return 34 &#9;&#9;} &#9;&#9; &#9;&#9;var height: CGFloat { &#9;&#9;&#9;&#9;if UIDevice.current.userInterfaceIdiom == .pad { &#9;&#9;&#9;&#9;&#9;&#9;return 0.15 &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;return 0.15 &#9;&#9;} &#9;&#9; &#9;&#9;var weight: CGFloat { &#9;&#9;&#9;&#9;if UIDevice.current.userInterfaceIdiom == .pad { &#9;&#9;&#9;&#9;&#9;&#9;return 0.44 &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;return 0.43 &#9;&#9;} &#9;&#9; &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;NavigationView{ &#9;&#9;&#9;&#9;&#9;&#9;GeometryReader { geometry in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;ScrollView(.vertical, showsIndicators: true) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;LazyVGrid(columns: gridItemLayout, spacing: 18) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Group { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;ForEach(0..<views.count) { i in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text(viewTexts[i]) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.foregroundColor(.black) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.frame(width: geometry.size.width * weight, height: geometry.size.height * height) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.background(Color.white) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.onTapGesture { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;views[i] = true &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.sheet(isPresented: $views[i]) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Views[i] &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;}.padding() &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;.navigationTitle("Title") &#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;} &#9;&#9;} &#9;&#9; } struct View1: View { &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;Text("View1") &#9;&#9;} } struct View2: View { &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;Text("View2") &#9;&#9;} } struct View3: View { &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;Text("View3") &#9;&#9;} } struct View4: View { &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;Text("View4") &#9;&#9;} }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20
Reply to How can I have a variable that stores a few different struct types?
Unless I miss something, it looks like you want some inheritance on your struct. Why not use class instead ? class Test { &#9;&#9;var id: Int = 0 } class TestOne : Test { &#9;&#9;var name: String = "" &#9;&#9;var colour: String = "clear" &#9;&#9;static var text: String = "TestOne" } class TestTwo : Test { &#9;&#9;var lesson: String = "" &#9;&#9;var description: String = "lesson" &#9;&#9;static var text: String = "TestTwo" } struct ChosenTest { &#9;&#9;var test1 = TestOne() &#9;&#9;var test2 = TestTwo() &#9;&#9;func printText() { &#9;&#9;&#9;&#9;print(type(of: test1).text, "and", type(of: test2).text) &#9;&#9;} } let testOne = TestOne() let testTwo = TestTwo() let chosen = ChosenTest(test1: testOne, test2: testTwo) chosen.printText() You get TestOne and TestTwo Note: you cannot declare the static var in Test, otherwise when you change in a subclass you change for all other subclasses
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’20
Reply to How can I have a variable that stores a few different struct types?
Or maybe you want this: class Test { &#9;&#9;var id: Int = 0 } class TestOne : Test { &#9;&#9;var name: String = "" &#9;&#9;var colour: String = "clear" &#9;&#9;static var text: String = "TestOne" } class TestTwo : Test { &#9;&#9;var lesson: String = "" &#9;&#9;var description: String = "lesson" &#9;&#9;static var text: String = "TestTwo" } struct UniversalTest { &#9;&#9;var test : Test &#9;&#9;func printText() { &#9;&#9;&#9;&#9;if let tested = test as? TestOne { &#9;&#9;&#9;&#9;&#9;&#9;print(type(of: tested).text) &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;if let tested = test as? TestTwo { &#9;&#9;&#9;&#9;&#9;&#9;print(type(of: tested).text) &#9;&#9;&#9;&#9;} &#9;&#9;} } var testUniversal = UniversalTest(test: testOne) testUniversal.printText() testUniversal = UniversalTest(test: testTwo) testUniversal.printText() and get TestOne TestTwo
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’20
Reply to How could I develop and deliver an offline library app with millions of images?
I must miss something in your question. You want user to get the full content. It is 1 + 17 GB (unless you compress the images) But you don't want user to download 18 GB AFAIK, that's not possible (yes, Xcode is 12 GB though…) https://developer.apple.com/forums/thread/659620 And you don't want to store data on a server for on request retrieval… Note: I would take care of performance with a 1 M rows tableView.
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’20
Reply to Improve SearchBar
If you confirm it works when you type one or more char, from the code here, no reason it doesn't work when you backspace. Problem is likely elsewhere. What is the result of parseJson ? Does it do some filtering on the array? Could you show the complete code for the class, specially parseJson ? Could you test also with some print: func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { &#9;&#9;if searchText == "" { &#9;&#9;&#9;parseJson() &#9;&#9;} else { &#9;&#9;&#9;&#9;print("searchText", searchText) &#9;&#9;&#9;&#9;cPlayerArr = cPlayerArr.filter({ (player) -> Bool in return &#9;&#9;&#9;&#9;&#9;&#9;&#9;player.yahooName.lowercased().contains(searchText.lowercased()) &#9;&#9;&#9;&#9;&#9;}) &#9;&#9;&#9;&#9;print("cPlayerArr", cPlayerArr) &#9;&#9;} &#9;&#9;collections.reloadData() &#9;} Note : retesting on line 4 for "" is not needed.
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’20