Post

Replies

Boosts

Views

Activity

Reply to Using keypaths to add a value to a variable?
You have selectedProduct = "Academia blueBandages" What is really selected ? a product (with all properties: brand, option1…) or an item in inventory ? In Product, some information seems redundant : the option (color) and the item (bandage) Why do you need both ? Why don't you have an array for items ? struct Product { 		var brand: String 		var option1: String // is it really needed ? You can infer from the item. 		var option2: String 		var items: [String] } What do you want to do ? user select a Product => then you know which items are selected what do you want to do in inventory ? remove 1 item ? Why do you add ? Anyway, with a dictionary as var inventory: [String: Int] = [ 		"blueBandages": 0, 		"redBandages": 0, 		"yellowBandages": 0, // Need comma here 		"greenBandages": 0 ] Then let product1 = Product(brand: "Academia", option1: "Blue", option2: "Red", items: ["blueBandages", "redBandages"]) let product2 = Product(brand: "Academia", option1: "Yellow", option2: "Green", items: ["yellowBandages", "greenBandages"]) let products = [product1, product2] func buy(product: Product) { 		 		for item in product.items { 				inventory[item]? += 1 		} } buy(product: products[0]) print(inventory) buy(product: products[1]) print(inventory) gives ["greenBandages": 0, "redBandages": 1, "blueBandages": 1, "yellowBandages": 0] ["greenBandages": 1, "redBandages": 1, "blueBandages": 1, "yellowBandages": 1]
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21
Reply to Using keypaths to add a value to a variable?
To derive color from items: let bandColors: [String: String] = [ 		"blueBandages": "Blue", 		"redBandages": "Red", 		"yellowBandages": "Yellow", 		"greenBandages": "Green" ] struct Product { 		var brand: String 		var items: [String] 		var colors: [String] { 				items.map { bandColors[$0] ?? "None"} 		} } var inventory: [String: Int] = [ 		"blueBandages": 0, 		"redBandages": 0, 		"yellowBandages": 0, 		"greenBandages": 0 ] let product1 = Product(brand: "Academia", items: ["blueBandages", "redBandages"]) let product2 = Product(brand: "Academia", items: ["yellowBandages", "greenBandages"]) let products = [product1, product2] func buy(product: Product) { 		 		for item in product.items { 				inventory[item]? += 1 		} } print(products[0].colors) buy(product: products[0]) print(inventory) buy(product: products[1]) print(inventory) Which gives: ["Blue", "Red"] ["yellowBandages": 0, "redBandages": 1, "greenBandages": 0, "blueBandages": 1] ["yellowBandages": 1, "redBandages": 1, "greenBandages": 1, "blueBandages": 1]
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21
Reply to Swift - How to update ProgressView using while loop?
Update must occur in main thread func update() { &#9;&#9;&#9;&#9;DispatchQueue.global().async { &#9;&#9;&#9;&#9;&#9;&#9;var current:Int = 0 &#9;&#9;&#9;&#9;&#9;&#9;let total = 100 &#9;&#9;&#9;&#9;&#9;&#9;while (current < total) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;current += 1                 DispatchQueue.main.async {  // To update UI &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.progress.setProgress(Float(current / total), animated: true) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; self.text.text = "Hello: " + String(current) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; print(Float(current / total)) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;sleep(UInt32(0.1)) &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;} &#9;&#9;}
Topic: UI Frameworks SubTopic: UIKit Tags:
Jan ’21
Reply to tableview.reloadRows() takes three hit to reload
A possible problem is that, when you resect an already selected cell, didSelect is not called. Just a deselect is called. Did you allow for multiple selections ? So, you should implement func tableView(UITableView, didDeselectRowAt: IndexPath) as well How is dummyData[indexPath.row].done set at first ? I suppose all values gave done == false. Please show how you declared and initialized. Why do you recompute indexPath ? todoTableView.reloadRows(at: [indexPath], with: .none) Should be enough (I assume there is only one section) Note: There is no need to reset the cell delegate, but that should not be the problem.
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21
Reply to Swift - Float returns wrong value.
Swift is not complicated, but you have to take care of its strong typing. current and total are Int hence current / total is inferred as Int, and the Int result or 75/100 is 0 Just write: let calc : Float = Float(current) / Float(total) Or declare them as Float: let current : Float = 75, total : Float = 100 let calc : Float = current / total print(calc)
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21
Reply to tableview.reloadRows() takes three hit to reload
May you please let me know what do you mean by recompute indexPath? In this part I just use the indexPath from the didSelectRowsAt() method without making any changes to the indexPath.         let index = IndexPath(row: indexPath.row, section: 0) You could just use indexPath that is passed as parameter. May you let me know what you mean by "There is no need to reset the cell delegate?"         todoCell.delegate = self
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21