Post

Replies

Boosts

Views

Activity

Reply to SwiftUI : displaying a formatted dictionary from string.
You have to pass a State var as argument. I did not test this completely: struct ContentView: View { 		@Binding var data: String 		@State var decipheredString: String 		var body: some View { 				let deciphered = data.split(separator: "\n").reduce(into: [String: String]() { 						let str = $1.split(separator: ":") 						if let first = str.first, let value = str.last { 								$0[String(first)] = String(value) 						} 				} 				decipheredString = deciphered["lastname"] ?? "" 				return HStack { 						Text("Last Name") 						TextField("", text: $decipheredString) 				} 		} }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20
Reply to Improve SearchBar
When you insert 2 chars in search then backspace, what do you get from the print I proposed to insert ? My guess: when you start searching, you call parseJson() this initialise cPlayerArr when you add a char, you filter cPlayerArr but you never reset it, so when you backspace, you sub filter the filtered array But you should at least keep the existing filtered elements. hence, I would need to see the result of the prints, at each iteration. So could you print as well, in func searchBar         print("search for: ", searchText, searchText.count) To check if the backspace is interpreted as some additional char (maybe you have something specific in a textField delegate func ?) Another test would be to redo a parse (just for test): func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {     if searchText == "" {       parseJson()     } else	{       parseJson() // JUST To reset cPlayerArr and see if it changes anything       cPlayerArr = cPlayerArr.filter({ (player) -> Bool in return         player.yahooName.lowercased().contains(searchText.lowercased())       })     }     collections.reloadData()   } Could you also post all searchBar related func the itemAtRow func for the collectionView ?
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’20
Reply to IB Designables Failed to render and update auto layout status for ViewController
@macke  I tested in Xcode 12.2. MacOS Catalina 10.15.7 I added a red background for the layer to properly see it. 		 private func commonInit() { 					self.clipsToBounds = true 					self.backgroundColor = .red 					self.layer.cornerRadius = self.bounds.size.height / 2 		 } At first, didn't work, even though Designables were declared up to date. I closed the project and reopened, then it was OK, I did see the red, roundedcorner UIButton. Works as well on Xcode 12.3.
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’20
Reply to SwiftUI : displaying a formatted dictionary from string.
Does the code you show compile ? 								HStack { 										Text("Last name") 										TextField("", text: deciphered["lastName"] ?? "") 								} You could create a struct : 	 struct ItemData { 	   var firstName: String = "" 	   var lastname: String = "" 	   var gender: String = "" 	   var age = 0 	 } Set a Binding     @Binding var itemData: ItemData	 and use in TextField                 TextField("firstName", text: $itemData.firstName) See another example here: https://developer.apple.com/forums/thread/669852
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20
Reply to Dismiss Keyboard SwitUI
Would this be a solution ? UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) https ://www.hackingwithswift .com/quick-start/swiftui/how-to-dismiss-the-keyboard-for-a-textfield Or do search to hide as soon as you leave the field ? In UIKit, I had to add a return button to the pad.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20
Reply to Improve SearchBar
for my print("search for: ", searchText, searchText.count) it shows the exact amount of characters and the word in the searchbar even when I delete characters. So, if you type end in search, you get end 3 after backspace en 2 Correct ? If so, that means there is no issue with the search string. I added the parseJson() right before the filter and it paused it like you said and reset the array. Is the array filtered or you get the complete array, even though search is not empty (en in the example above). If you get the complete array, that means problem is elsewhere. Could you show the numberOfItemsInSection dataSource func ? And add a print inside     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { print("Number of cells", cPlayerArr.count) return cPlayerArr.count // I assume }
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’20
Reply to set individual information to each custom marker
Code will be easier to analyse when formatted and pasted as Paste and Match Style and without useless blank lines func setUpAllThePins(allLogos:[String], allLongitudesAndLatitudes:[CLLocationCoordinate2D], kmDouble:[Double], streets:[String], alcaldia:[String], calificacion:[Int], titulos:[String]) { 		 		imagenes = allLogos.compactMap { link->UIImage? in 				guard let url = URL(string: link) else {return nil } 				guard let imageData = try? Data(contentsOf: url) else {return nil } 				return UIImage(data: imageData) 		} 		 		let annotations = zip(allLongitudesAndLatitudes, imagenes) 				.map { (coordinate, allLogos) -> myAnnotation in 						let annotation = myAnnotation(title: "", subtitle: "", coordinate: coordinate, localImage: allLogos) 						return annotation 				} 		 		if let oldArray = annotations as? [MKAnnotation] { 				for i in oldArray{ 						mapView.addAnnotation(i) 				} 				 		} 		 } I have a problem with an array of MKAnnotationViews that are customized, in which I want to display the information for each marker individually, such as its title or its description that contains that marker, I don't know if it opens any way to do it. * What is exactly the problem ? What is the information you want to display ? titles ? you pass nothing in annotation on line 11. Why ? What should be title and subtitle ?
Topic: UI Frameworks SubTopic: UIKit Tags:
Dec ’20
Reply to Improve SearchBar
Alright I figured it out finally I had to make another array to filter my JSON data. Have 2 arrays makes it sure you start filtering from a complete array. But resetting by doing a parseJson() call each time should have produced a similar effect. So maybe you have another problem somewhere… Or you changed something else when adding the second array.
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’20
Reply to How can I have a variable that stores a few different struct types?
So, if you want inheritance, just use class. IMHO, you will never notice memory issue because of it, may even be reverse. And passing objects by reference may avoid copy content, hence saving memory. But once again, that is not an issue here. If you are interested in class vs struct comparison, there are tons of articles available. Just one here: https ://medium. com/@linhairui19/struct-vs-class-understanding-swift-performance-part-1-f46a6811c8f2
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’20