Swift List Product
A possible solution (now updated for Swift 2):
let rows = ["a", "b", "c"]
let cols = ["1", "2", "3"]
let squares = rows.flatMap {
row in
cols.map {
col in
row + col
}
}
print(squares)
// [a1, a2, a3, b1, b2, b3, c1, c2, c3]
The inner map()
maps the cols
array to an array with the
row number prepended to each entry. The outer flatMap()
maps
the rows
array to an array of arrays (one for each row) and flattens the result.
Slightly more general, one could define the "product" of two
sequences as an array of tuples (pairs) with all combinations:
func product<S : SequenceType, T : SequenceType>(lseq : S, _ rseq : T) -> [(S.Generator.Element, T.Generator.Element)] {
return lseq.flatMap { left in
rseq.map { right in
(left, right)
}
}
}
and then use it as
let squares = product(rows, cols).map(+)
Add a product in my shopping list - Creating a shopping list app
I see no outlet between your view controllers. Keep in mind that you can only show one view controller at a time. I can also see that you created a big view controller hardly. You should do it programmatically...
Go on this page : https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/CreateATableView.html , you'll learn how to create Table View, it will be really usefull for what you are trying to do.
Then you will have to pass data between controller so i suggest you go there : https://learnappmaking.com/pass-data-between-view-controllers-swift-how-to/
Swift: How To Reduce List of Non-Unique Products and Sum Quantities
You can group by the product name, put them all into a dictionary, then sum up the values.
let dict = Dictionary(grouping: originalList,
by: { $0.productName })
.mapValues { $0.reduce(0, { $0 + $1.qty }) }
This gives you a [String: Int]
. If you want a [(productName: String, qty: Int)]
instead,
let entries = dict.map { (productName: $0, qty: $1) }
Show Quantity label when product is in cart(Swift 5)
Assuming that your card is an array of Product
, you can check in your cellForRowAt indexPath
whether the item at current index is present in your cart and show/hide buttons accordingly.
let productAtIndex = self.productArray[indexPath.row]
if cart.contains(where: { (item) -> Bool in
productAtIndex.id == item.id
}){
// product is in cart, show increment/decrement button
} else {
// product not in cart, show add button
}
Related Topics
Core Data Appears to Lose Data After Xcode Upgrade
Swift Video to Document Directory
Swift - Nsdate - Remove Part of Date
Swiftui Vertically Misaligned Text
How to Build a Recursive Function in Swift to Return a String
Swift 4 JSONdecoder Optional Variable
How to Programmatically Change The Xdr Display Reference Mode (Aka Preset) on Macos
Swift Sha256 Encryption Returns Different Encrypted String Compare to Objective C
Swiftui Navigationview Not See Image
Lldb for Swift: Access Computed Property or Perform Function Call in Type Summary Python Script
How to Set Default Clouse Param in View Method
Compiling for iOS 10.3, But Module 'swiftuicharts' Has a Minimum Deployment Target of iOS 13.0
Optional Protocol Requirements, I Can't Get It to Work
Saving And/Or Querying User Display Names in Firebase Using Caseinsensitive
How to Reconnect Akplayer and Akmixer After Audiokit.Stop()