Post

Replies

Boosts

Views

Activity

Reply to Programmatically closing an app.
I tested this in simulator and it worked:           DispatchQueue.main.asyncAfter(deadline: .now()) {                   UIApplication.shared.perform(#selector(NSXPCConnection.suspend))               } But not sure that would pass the appstore review https://stackoverflow.com/questions/13989030/is-there-any-way-to-programmatically-send-my-iphone-app-to-the-background
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to E-post adress
If you want to contact someone, first make sure to post your precise question on the forum. If you have no temporary address as eskimo advised, post your address and remove it by editing the post rapidly. After 1 hour, you will not be able to edit or remove the post and your address would remain permanently visible.
Apr ’21
Reply to In App Settings
When you paste code, please use Paste And Match Style to avoid all the blank lines that make code very hard to read. import UIKit import SafariServices import UserNotifications class Home: UIViewController { 		 		@IBOutlet weak var leadingHome: NSLayoutConstraint! 		@IBOutlet weak var trailingHome: NSLayoutConstraint! 		 		var menuOut = false 		 		static var riddleNotification = Bool() 		 		override func viewDidLoad() { 				super.viewDidLoad() 				 				//Ask for Notification Permision 				let center = UNUserNotificationCenter.current() 				 				center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in 				} 				 				func riddleNotification() { 						 						//Notification Content 						let content = UNMutableNotificationContent() 						content.title = "Check out this weeks riddle!" 						content.body = "This weeks riddle is..." 						content.categoryIdentifier = "riddle" 						 						//Notification Trigger 						let date = Date().addingTimeInterval(5) 						 						var dateComponents = DateComponents() 						//dateComponents.hour = 9 						//dateComponents.minute = 30 						//dateComponents.weekday = 4 //Wednesday 						let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true) 						 						//Create Request 						 						let uuidString = UUID().uuidString 						let request = UNNotificationRequest(identifier: uuidString, content: content, trigger: trigger) 						 						//Register Request 						center.add(request) { (error) in 								//Check the parameter and handle any errors 						} 				} 				 				 				if let value = UDM.shared.defaults.value(forKey: "riddleNotification") as? Bool { 						Home.riddleNotification = value 				} 				if Home.riddleNotification == true { 						riddleNotification() 				} 				else if Home.riddleNotification == false { 						print("no riddle notification scheduled") 				} 				else { 						 						let isRegisteredForRemoteNotifications = UIApplication.shared.isRegisteredForRemoteNotifications 						if isRegisteredForRemoteNotifications { 								// User is registered for notification 								Home.riddleNotification = true 								UDM.shared.defaults.setValue(Home.riddleNotification, forKey: "riddleNotification") 						} 						else { 								// Show alert user is not registered for notification 								Home.riddleNotification = false 								UDM.shared.defaults.setValue(Home.riddleNotification, forKey: "riddleNotification") 						} 						riddleNotification() 				} 		} 		 		@IBAction func Riddles(_ sender: Any) { 				let vc = SFSafariViewController(url: URL(string: "https://riddles.com")!) 				 				present(vc, animated: true) 		} 		 		@IBAction func RiddlesandAnswers(_ sender: Any) { 				let vc = SFSafariViewController(url: URL(string: "https://riddlesandanswers.com")!) 				 				present(vc, animated: true) 		} 		 		@IBAction func menuTappedHome(_ sender: Any) { 				 				if menuOut == false { 						leadingHome.constant = 150 						trailingHome.constant = -150 						menuOut = true 				} 				else { 						leadingHome.constant = 0 						trailingHome.constant = 0 						menuOut = false 				} 				UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseIn, animations: { 						self.view.layoutIfNeeded() 				}) { (animationComplete) in 						print("The animation is complete") 				} 		} 		 } class UDM { 		 		static let shared = UDM() 		 		let defaults = UserDefaults(suiteName: "com.riddlewednesday.saved.data")! 		 } class Settings: UIViewController { 		 		@IBOutlet weak var leading_Settings: NSLayoutConstraint! 		@IBOutlet weak var trailing_Settings: NSLayoutConstraint! 		@IBOutlet var riddleSwitch: UISwitch! 		 		var menuOut = false 		 		override func viewDidLoad() { 				super.viewDidLoad() 				 				if Home.riddleNotification == true { 						riddleSwitch.setOn(true, animated: true) 				} 				 				if Home.riddleNotification == false { 						riddleSwitch.setOn(false, animated: true) 				} 		} 		 		@IBAction func menuTappedSettings(_ sender: Any) { 				 				if menuOut == false { 						leading_Settings.constant = 150 						trailing_Settings.constant = -150 						menuOut = true 				} 				else { 						leading_Settings.constant = 0 						trailing_Settings.constant = 0 						menuOut = false 				} 				UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseIn, animations: { 						self.view.layoutIfNeeded() 				}) { (animationComplete) in 						print("The animation is complete") 				} 		} 		 		@IBAction func riddleSwitchDidChange(_ sender: Any) { 				if riddleSwitch.isOn { 						 						Home.riddleNotification = true 						UDM.shared.defaults.setValue(Home.riddleNotification, forKey: "riddleNotification") 				} 				else { 						Home.riddleNotification = false 						UDM.shared.defaults.setValue(Home.riddleNotification, forKey: "riddleNotification") 				} 		} 		 } I continue in a second answer for authorisation request.
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to How do I stop 'The compiler is unable to type-check this expression in reasonable time'
If not too long, it is easier to Paste And Match Style for the code. You get line numbers directly. import SwiftUI import SDWebImage import HalfModal import Foundation struct rocketView: View { 		 		@State var rockets: [Rocket] = [] 		@State private var showingHalfModal2: Bool = false 		 		var body: some View { 				ZStack { 						NavigationView { 								VStack { 										Spacer() 												.frame(height: 1) 												.navigationBarTitle("Rockets") 										 										List { 												 												ForEach(rockets, id: \.id) { rocket in 														 														Button(action: { 																withAnimation { 																		self.showingHalfModal2 = true 																} 														}) { 																rHStack(rocket) 														} 														.buttonStyle(PlainButtonStyle()) 												} 										} 								}.onAppear { 										rocketApiCall().getUsers{ (rockets) in rockets = rockets} 								}.listStyle(SidebarListStyle()) 								.frame(alignment: .center) 						} 				} 				if showingHalfModal2 { 						HalfModalView(content: AnyView(HStack { 								VStack { 										Text("test123") 												.padding() 								} 						}), header: AnyView(HStack { 																		VStack(alignment: .leading) { 																				Text("test") 																				Text("test") 																						.font(.system(size: 10)) 																						.foregroundColor(Color.gray) 																		}}), isPresented: $showingHalfModal2) 				} 		} 		 		func rHStack(_ rocket: Rocket) -> some View { 				HStack { 						 						Group { 								Text(rocket.name) 										.font(.system(size: 23)) 										.frame(maxWidth: .infinity, alignment: .leading) 										.fixedSize(horizontal: false, vertical: true) 								Text("Active: " + String(rocket.active)) 										.font(.system(size: 15)) 										.foregroundColor(Color.gray) 								Spacer() 						} 				} 		} } struct rocketView_Previews: PreviewProvider { 		static var previews: some View { 				rocketView() 		} } I have moved quite a chunk of code to a new Swift file called rocketView, it got rid of the original error, but now I get the error Cannot convert value of type '(inout [Rocket]) -> ()' to expected argument type '([Rocket]) -> ()' on line 45... my code is attached. (Line 45 is the line between }.onAppear { and }.listStyle... So the error is now line 34. But you don't show the code for rocketApiCall() and its getUsers So it's hard to say. Note: it is more readable to give a different name anyway, such as: rocketApiCall().getUsers { (theRockets) in rockets = theRockets} Try also removing the parenthesis around theRockets
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to Reduce/replace loops in my conditional checks ? [CoreData]
A limited simplification is to use where clause: replace &#9;&#9;&#9;&#9;&#9;&#9;for person in personAvailable { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;//PERSONAL REQUEST : SPECIFIC WORK DAY &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;if person.avilability == work.day { with &#9;&#9;&#9;&#9;&#9;&#9;for person in personAvailable where person.avilability == work.day { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;//PERSONAL REQUEST : SPECIFIC WORK DAY So that would allow to write more compact code: &#9;&#9;&#9;&#9;for person in personAvailable where person.avilability == work.day { &#9;&#9;&#9;&#9;&#9;&#9;&#9;for personRequirement in person.materialsNeeded where personRequirement.tool == toolsAvailable.name && personRequirement.quantity <= toolsAvailable.stock && !personRequirement.met { that replaces &#9;&#9;&#9;&#9;&#9;&#9;for person in personAvailable { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;if person.avilability == work.day { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;for personRequirement in person.materialsNeeded { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;if personRequirement.tool == toolsAvailable.name { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;if personRequirement.quantity <= toolsAvailable.stock && personRequirement.met == false { But I fear you have to go through the loops anyway.
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to Searching for the position of an item in an array within an array?
You could do something like this: struct Product { &#9;&#9;var brand: String &#9;&#9;var option: [String] &#9;&#9;var item: [String] } let products = [ &#9;&#9;Product(brand: "Academia", option: ["Blue", "Red"], item: ["academiaBandagesBlue", "academiaBandagesRed"]), &#9;&#9;Product(brand: "Academia", option: ["Purple", "Green"], item: ["academiaBandagesPurple", "academiaBandagesGreen"]) ] var tackInventory: [ String : Int] = [ &#9; "academiaBandagesBlue" : 1, &#9;&#9;"academiaBandagesGreen" : 0, &#9;&#9;"academiaBandagesPurple" : 0, &#9;&#9;"academiaBandagesRed" : 0 ] let filteredKeys = tackInventory.filter { $0.value > 0}.map {$0.key} // Select the keys to search for print(filteredKeys) for key in filteredKeys { &#9;&#9;let filteredProducts = products.filter { $0.item.contains(key)} // all the products that contain that specific item &#9;&#9;for product in filteredProducts { &#9;&#9;&#9;&#9;if let itemPosition = product.item.firstIndex(of: key) { // What is the position of key in item list (there should be only one ? &#9;&#9;&#9;&#9;&#9;&#9;let foundColor = product.option[itemPosition] // If color are in the same order than item, then we got it &#9;&#9;&#9;&#9;&#9;&#9;print("key", key, "color", foundColor) &#9;&#9;&#9;&#9;} &#9;&#9;} } and get: ["academiaBandagesBlue"] key academiaBandagesBlue color Blue But your coding of Product is not robust at all. If you do not put color and item in same order, you get wrong result. You'd better have a dictionary linking item with its color. You could also define item as a couple of String: struct Product { &#9;&#9;var brand: String &#9;&#9;var item: [(color: String, ref: String)] } let products = [ &#9;&#9;Product(brand: "Academia", item: [("Blue", "academiaBandagesBlue"), ("Red", "academiaBandagesRed")]), &#9;&#9;Product(brand: "Academia", item: [("Purple", "academiaBandagesPurple"), ("Green", "academiaBandagesGreen")]) ] var tackInventory: [ String : Int] = [ &#9; "academiaBandagesBlue" : 1, &#9;&#9;"academiaBandagesGreen" : 0, &#9;&#9;"academiaBandagesPurple" : 0, &#9;&#9;"academiaBandagesRed" : 0 ] let filteredKeys = tackInventory.filter { $0.value > 0}.map {$0.key}&#9;&#9;&#9;&#9;// Select the keys to search for for key in filteredKeys { &#9;&#9;let filteredProducts = products.filter { product in &#9;&#9;&#9;&#9;for item in product.item { if item.ref == key { return true }} &#9;&#9;&#9;&#9;return false &#9;&#9;} //all the products that contain that specific item &#9;&#9;for product in filteredProducts { &#9;&#9;&#9;&#9;for item in product.item where item.ref == key { &#9;&#9;&#9;&#9;&#9;&#9;let foundColor = item.color &#9;&#9;&#9;&#9;&#9;&#9;print("key", key, "ref", item.ref, "color", foundColor) &#9;&#9;&#9;&#9;} &#9;&#9;} } and get key academiaBandagesBlue ref academiaBandagesBlue color Blue
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to Searching for the position of an item in an array within an array?
You should give an example ot the array and what you want to find position of, that would be easier to understand. Is it the one you had in other post ? let products = [ &#9;&#9;Product(brand: "Academia", option: ["Blue", "Red"], item: ["academiaBandagesBlue", "academiaBandagesRed"]), &#9;&#9;Product(brand: "Academia", option: ["Purple", "Green"], item: ["academiaBandagesPurple", "academiaBandagesGreen"]) ]  I want to be able to use the keys of the dictionary to "look up" the colorName within the product catalog using the name of the item found in the dictionary. Please confirm the following: you use the key in inventory, such as "academiaBandagesBlue" ?         Which key do you select ? The one with non null value ? You look for products that contain the item ? Then you get the color ? I don't understand what the products are? Product(brand: "Academia", category: "Bandages", colorName: [ "Blue", "Green", "Purple", "Red" ], item: ["academiaBandagesBlue", "academiaBandagesGreen", "academiaBandagesPurple", "academiaBandagesRed"]) Is it the only product ? It has all items and all colors. How will you match color and item ?
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to Creating Multiple Bodies
Is it what you are looking for ? struct ContentView: View { let exampleVariable = "Example" func exampleFunc() { print("Example") } var body: some View { Text("Hello") secondBody } var secondBody: some View { Text("World") } } struct ContentView_Previews: PreviewProvider { static var previews: some View { Group { ContentView() .previewDisplayName("iPod Touch") } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to Get a random word object. (swift, parse)
parse database What do you mean, precisely ? do you get a return from a request ? which request ? what is the return you want to get a random of what exactly ? Don't ask such general question on the forum: explain what you want to achieve post some initial code explain what problem you are facing Did you first look at Parse tutorials, to learn how to use ? As an example: https ://guides.codepath. com/ios/Building-Data-driven-Apps-with-Parse Then, if you have a query: query.findObjectsInBackground { (posts: [Post]?, error: Error?) in if let posts = posts { // do something with the array of object returned by the call } else { print(error?.localizedDescription) } } you can get a random item in array with: if let randomPost = posts..randomElement() { // Do what you need of this random value }
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21