Post

Replies

Boosts

Views

Activity

Reply to xcode
First, find the appropriate version from Xcode wiki - https://en.wikipedia.org/wiki/Xcode. Seems the latest version of Xcode which runs on High Sierra 10.13.6 is 10.1. You can download old Xcodes from More Downloads page - https://developer.apple.com/download/more/. But please remember, you need Xcode 11 or later to build an iOS app for App Store, meaning you need to upgrade your macOS (or Mac) to 10.14.4 or later to build App Store apps.
Apr ’21
Reply to Deleting views from a stack view?
Please try this: 		func removeAllButtons() { 				let buttons = tackStack.arrangedSubviews 						.filter {$0 is UIButton} 				for button in buttons { 						tackStack.removeArrangedSubview(button) 						button.removeFromSuperview() 				} 		}
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to Anyway to remove the word 'Optional' from a website obtained from Google API?
the printed out value is: Optional("Website URL"). Assuming you get this output from print("The selected place is: \(String(describing: place.website ))"), you may need to unwrap Optional in a safe manner: if let place = place, let website = place.website { print("The selected place is: \(website)") } This is not a critical issue, but you should better use error instead of error.localizedDescription to print debugging info. if let error = error { print("An error occurred: \(error)") return } print("....\(error)") will give you more info than error.localizedDescription.
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to Problem With Filter
Thanks for showing your code. Checking your dataSource methods, your UICollectionView (collections) shows allTextArr when searchBar.selectedScopeButtonIndex 0. But in your searchBar(_:textDidChange:), you update allTextArr from cPlayerArr. Generally: You need to re-fetch when selectedScopeButtonIndex is changed You need to re-filter when searchText is changed But your current searchBar(_:textDidChange:) mixes up two different things -- re-fetch and re-filter. I would write them as follows: // Array of fetched `CurrentPlayers` when `selectedScopeButtonIndex 0` var origPlayerArr = [CurrentPlayers]() func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { updateFilteredArray(searchText: searchText) collections.reloadData() } private func updateFilteredArray(searchText: String) { let origArray: [CurrentPlayers] if searchBar.selectedScopeButtonIndex 0 { origArray = origPlayerArr } else { origArray = cPlayerArr } allTextArr = origArray.filter { player - Bool in if searchText.isEmpty { return true } else { return player.yahooName!.lowercased().contains(searchText.lowercased()) } } } func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange: Int) { if (searchBar.selectedScopeButtonIndex == 0) { cPlayerArr.removeAll() allTextArr.removeAll() fetchAllPlayers() } else if (searchBar.selectedScopeButtonIndex == 1) { allTextArr.removeAll() fetchForwards() } else if (searchBar.selectedScopeButtonIndex == 2) { allTextArr.removeAll() fetchDefense() } else { allTextArr.removeAll() fetchGoalies() } updateFilteredArray(searchText: searchBar.text ?? "") collections.reloadData() } And modify fetch methods fetchForwards(), fetchDefense() and fetchGoalies() as: func fetchForwards() { do { let request = CurrentPlayers.fetchRequest() as NSFetchRequestCurrentPlayers let forwards = NSPredicate(format: "position CONTAINS 'RW' OR position CONTAINS 'LW' OR position CONTAINS 'C'") request.predicate = forwards self.origPlayerArr = try context.fetch(request) //- } catch { print(error) //... } } // Same for `fetchDefense()` and `fetchGoalies()`...
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to Question: Swiftui http request without button
Thanks for updating your code. It gets far more readable and would save readers time. (When you see extra empty lines than your original code, Edit > Paste and Match Style of Safari would work.) In your code, you put too many lines in an action closure, you should better create a method and just call it inside an action closure. struct ContentView: View { 		 		@State var username: String = "." 		@State var password: String = "." 		 		var body: some View { 				ZStack { 						VStack { 								.onAppear { //-> Cannot infer contextual base in reference to member 'onAppear' 										doHttpRequest() 								} 								//... 						} 				} 		} 		 		func doHttpRequest() { 				if 1==1 { //Do you still need this? 						let myUrl = URL(string: "...")! //Trailing semicolon is not needed 						var request = URLRequest(url: myUrl) 						request.httpMethod = "POST"// Compose a query string 						let postString = "Name=\($username)&Passwort=\($password)" //??? 						 						request.httpBody = postString.data(using: .utf8) 						 						let task = URLSession.shared.dataTask(with: request) { 								(data, response, error) in 								 								//Use if-let when you want to use the unwrapped value 								if let error = error { 										print("error=\(error)") 										return 								} 								 								//Use guard-let when nil has no meaning and want to exit on nil 								guard let response = response else { 										print("Unexpected nil response") 										return 								} 								// You can print out response object 								print("response = \(response)") 								 								//Let's convert response sent from a server side script to a NSDictionary object: 								do { 										//Use guard-let when nil has no meaning and want to exit on nil 										guard let data = data else { 												print("Unexpected nil data") 												return 										} 										//#1 `mutableContainer` has no meaning in Swift 										//#2 Use Swift Dictionary type instead of `NSDictionary` 										let json = try JSONSerialization.jsonObject(with: data) as? [String: Any] 										 										if let parseJSON = json { 												// Now we can access value of First Name by its key 												//Use if-let when you want to use the unwrapped value 												if let firstNameValue = parseJSON["Name"] as? String { 														print("firstNameValue: \(firstNameValue)") 														let dateien =	firstNameValue.components(separatedBy: ",") 														print(dateien) 												} 										} 								} catch { 										print(error) 								} 						} 						task.resume() 				} 		} } (I have brushed up some parts of doHttpRequest. Please see comments with //#.) You should better include the error messages when you want to ask something about codes causing errors. The error Cannot infer contextual base in reference to member 'onAppear' occurs becauseonAppear is a method classified as view modifier, which must be added to a view. In your code, there is no view directly before .onAppear. For example, if you put .onAppear to the outermost view of body, you can write it as: 		var body: some View { 				ZStack { 						VStack { 								//... 						} 				} 				.onAppear { //This `onAppear` is added to `ZStack{...}` 						doHttpRequest() 				} 		}
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to Adding Trailing Item to NavigationView Changes Appearance
I assume you mean navigationBarItems with trailing item. (It adds a trailing item to navigation bar.) I wanted to know why adding a trailing item messes everything up.  Not clearly documented, but the style of container may affect the default styles of content in SwiftUI. Please try specifying the listStyle explicitly: &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;NavigationView { &#9;&#9;&#9;&#9;&#9;&#9;List { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;//... &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;.listStyle(InsetListStyle()) //<- &#9;&#9;&#9;&#9;&#9;&#9;.navigationBarTitle("Grades") &#9;&#9;&#9;&#9;&#9;&#9;.navigationBarItems(trailing: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Button(action: {}) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Image(systemName: "plus") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;) &#9;&#9;&#9;&#9;&#9;&#9;.navigationViewStyle(StackNavigationViewStyle()) &#9;&#9;&#9;&#9;} &#9;&#9;}
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to Using Swift playground UIKit, Constraints are not working
An interesting issue. It seems as if iOS (or Playground?) is removing some constraints on the properties after loadView(). Is there any solution to fix this problem? Setting up subviews in viewDidLoad() would fix the issue: class ViewController2: UIViewController { var titleLabel: UILabel! var subLabel: UILabel! override func loadView() { let view = UIView(frame: viewRect) view.backgroundColor = UIColor.white self.view = view } override func viewDidLoad() { super.viewDidLoad() titleLabel = UILabel() titleLabel.translatesAutoresizingMaskIntoConstraints = false titleLabel.text = "title" titleLabel.backgroundColor = .gray view.addSubview(titleLabel) NSLayoutConstraint.activate([ titleLabel.widthAnchor.constraint(equalToConstant: 200), titleLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 100), titleLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor) ]) subLabel = UILabel() subLabel.translatesAutoresizingMaskIntoConstraints = false subLabel.text = "title" subLabel.backgroundColor = .red view.addSubview(subLabel) NSLayoutConstraint.activate([ subLabel.widthAnchor.constraint(equalToConstant: 100), subLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 100), subLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor) ]) } } But I do not understand what's happening behind and am not sure if this can be the solution for you.
Topic: UI Frameworks SubTopic: UIKit Tags:
Apr ’21
Reply to unrecognized selector sent to instance 0x144b071
Here is the error: I'm not sure what's happening, but the error message is hidden and we cannot see it. I needed to explore the HTML source for it: [CalorieCounter.FirstViewController yourAge:]: unrecognized selector sent to instance 0x144b071 This is very odd, as the selector yourAge: contains a colon. It seems as if iOS is invoking some action method (yourAge(_:) in Swift) linked to some UI component (maybe a UITextField). You once connected Action from the UITextField mistakenly? I guess you removed that mistakenly created action method from code, but you have not removed the connection in the Interface Builder. Open the Connections inspector, then find and remove the action connection showing yourAge: (grouped in Sent Evens).
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to insert Json file into core Data
The json is build as followed:  {1 : { "title" : "Mini pork pies with piccalilli", "category" : "meat"}, 2 : {"title" : "apple crumble", "category" : "dessert"}} First of all, {1 : { "title" : "Mini pork pies with piccalilli", "category" : "meat"}, 2 : {"title" : "apple crumble", "category" : "dessert"}} is not a valid JSON. If you want to work with Apple's frameworks, you first need to make a valid JSON. One possible form: { "1" : { "title" : "Mini pork pies with piccalilli", "category" : "meat" }, "2" : { "title" : "apple crumble", "category" : "dessert" } } (In valid JSON, the keys need to be JSON string, no number keys are allowed.) Another would be: [ { "title" : "Mini pork pies with piccalilli", "category" : "meat" }, { "title" : "apple crumble", "category" : "dessert" } ] If you were using the latter form, you would directly get an Array from the JSON. So I assume the first one. But in any way, if your JSON actually looks like {1 : { "title" : "Mini pork pies with piccalilli", ..., please create a valid JSON first. Assuming you got the JSON file as my first example, you can write some code to convert it to an Array like this: let jsonText = """ { "1" : { "title" : "Mini pork pies with piccalilli", "category" : "meat" }, "2" : { "title" : "apple crumble", "category" : "dessert" } } """ func jsonTextToArray(_ jsonText: String) - [[String: Any]]? { guard let data = jsonText.data(using: .utf8) else { print("invalid data") return nil } do { guard let json = try JSONSerialization.jsonObject(with: data) as? [String: [String: Any]] else { print("bad format") return nil } return json.sorted {Int($0.key) ?? 0 Int($1.key) ?? 0} .map {$0.value} } catch { print(error) return nil } } if let array = jsonTextToArray(jsonText) { print(array) } else { print("bad jsonText") } //-[["title": Mini pork pies with piccalilli, "category": meat], ["category": dessert, "title": apple crumble]]
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21