Set Index of Array (SwiftUI) - arrays

I am working through hackingwithswift doing the 100 Days of SwiftUI and currently on Day 35 which needs me to build a times table app.
Unfortunately, I have got stuck at a very early stage and feel stupid asking.
How can I make my array index start at 1 rather than 0?
Below is my code and screenshot of the canvas:
struct ContentView: View {
#State private var number = 0
#State private var question = Int.random(in: 0..<13)
let numberRange = Array<Int>(1 ... 12)
var body: some View {
VStack {
Picker("What times tables do you want to test your knowledge on?", selection: $number) {
ForEach(0 ..< numberRange.count) {
Text("\(self.numberRange[$0])")
}
}
.pickerStyle(SegmentedPickerStyle())
Text("What is \(number) x \(question) = ")
}
}
}

From Apple documentation:
The first element of a nonempty array is always at index zero.
Basically you'd need to add +1 to your number variable.
var correctNumber: Int {
number + 1
}
And use this correctNumber when displaying values or performing calculations.
Text("What is \(correctNumber) x \(question)?")
As correctNumber is a computed property it will always be up to date with number variable.

Related

Count string elements in an array in a multidimensional array

I have an array that I populate from firestore that uses a struct. Is there a way to count the number of times there is a matching string for the productName var.
This is my struct...
struct PlayerStock: Codable, Identifiable {
#DocumentID var id: String?
var productName: String
var qty: Int
var saleUID: String
var totalPrice: Int
var uid: String
var unitPrice: Int
}
This is what's in my VC, I populate this from firestore and then want to count matching strings in productName
var playerStock: [PlayerStock] = []
Is there a way to do this without using a for loop?
Strings I'd like to count in productName include "smartphone" or "laptop" I want to store the matching total count as an int like this:
var smartphoneTotal =
var laptopTotal =
etc etc..
I've tried using filters and compact map but can't find anything that works, I think its because the array is multidimensional or because its using a dictionary?
Pretty noob here so any help appreciated!
First group the array by productName
let groupedProducts = Dictionary.init(grouping: playerStock, by: \.productName)
you'll get
["smartphone":[PlayerStock(..), PlayerStock(..), PlayerStock(..)],
"laptop":[PlayerStock(..), PlayerStock(..)]
then map the values to their amount of items
.mapValues(\.count)
The result is
["smartphone":3, "laptop":2]
If you want to use filter, something like this should work with your struct:
var laptopTotal = playerStock.filter { $0.productName == "laptop" }.count
This may help
let wordsToFind = ["smartphone", "laptop"]
var foundCounts: [String: Int] = [:]
for p in playerStock {
for word in wordsToFind {
if p.name.contains(word) {
foundCounts[word] = foundCounts[word, default: 0] + 1
}
}
}
foundCounts
If you really want a functional "no for-loops" version, and if you mean you want to find things that contain your search terms, then:
let wordsToFind = ["smartphone", "laptop"]
let founds = wordsToFind.map { word -> (String, Int) in
playerStock.reduce(("", 0)) { partialResult, player in
(word, partialResult.1 + (player.name.contains(word) ? 1 : 0))
}
}
You could use the higher order functions filter() or reduce(). #ShawnFrank already gave an answer using filter(). (voted.)
For a small number of items, there isn't a big difference between filter() and reduce(). For large datasets, though, filter creates a second array containing all the items that match the filter criteria. Arrays are value types, so they hold copies of the entries they contain. This would increase the memory footprint needed to do the counting. (You'd have the original array and a copy containing all the matching elements in memory).
The higher order function reduce() works differently. it takes a starting value (a total starting at 0 in our case) for the result, and a closure. The closure takes the current result, and an element from the array you are parsing. At runtime, the reduce() function calls your closure over and over, passing in each element from the array you are reducing. In the first call to the closure, it sends the closure the initial value for result (a zero total, in our case.) In each subsequent call to the closure, it passes the result of the previous call. (The running total, for our implementation.) The reduce() function returns the result returned by the last call to your closure.
You can use reduce to count the number of items that match a given test without having to build a temporary array. Below is a sample implementation using reduce(). Note that I tweaked your PlayerStock type to add default values for all the properties other than productName since I don't care about those.
// Define the PlayerStock type, but use default values for everything but `productName`
struct PlayerStock: Codable, Identifiable {
var id: String? = nil
var productName: String
var qty: Int = Int.random(in: 1...10)
var saleUID: String = ""
var totalPrice: Int = Int.random(in: 10...200)
var uid: String = ""
var unitPrice: Int = Int.random(in: 10...200)
}
// Create an array of test data
let players = [
PlayerStock(productName: "smartphone"),
PlayerStock(productName: "CD Player"),
PlayerStock(productName: "laptop"),
PlayerStock(productName: "CD Player"),
PlayerStock(productName: "smartphone"),
PlayerStock(productName: "laptop"),
PlayerStock(productName: "smartphone"),
PlayerStock(productName: "boom box"),
PlayerStock(productName: "laptop"),
PlayerStock(productName: "smartphone"),
]
/// This is a function that counts and returns the number of PlayerStock items who's productName property matches a the string nameToFind.
/// If you pass in printResult = true, it logs its result for debugging.
/// - Parameter nameToFind: The `productName` to search for
/// - Parameter inArray: The array of `PlayerStock` items to search
/// - Parameter printResult: a debugging flag. If true, the function prints the count if items to the console. Defaults to `false`
/// - Returns: The number of `PlayerStock` items that have a `productName` == `nameToFind`
#discardableResult func countPlayers(nameToFind: String, inArray array: [PlayerStock], printResult: Bool = false) -> Int {
let count = array.reduce(0, { count, item in
item.productName == nameToFind ? count+1 : count
})
if printResult {
print("Found \(count) players with productName == \(nameToFind)")
}
return count
}
let smartphoneCount = countPlayers(nameToFind: "smartphone", inArray: players, printResult: true)
let laptopCount = countPlayers(nameToFind: "laptop", inArray: players, printResult: true)
let cdPlayerCount = countPlayers(nameToFind: "CD Player", inArray: players, printResult: true)
This sample code produces the following output:
Found 4 players with productName == smartphone
Found 3 players with productName == laptop
Found 2 players with productName == CD Player

How do I use a counter within body View to fill a list

I am new to Swift and try to get a feeling for it by trying new things. Now I got stuck with lists. I have two arrays, one is the product and the other the price for each product. I want to fill a list by using a ForEach loop. I use a HStack to have two columns.
To make it a bit more clear, here are the two arrays:
#state private var products = ["Coke", "Fanta", "Sprite", "Water]
private var prices = ["1,43$", "1,22$", "1,64$", "0,45$"]
Now this is a part of my ContentView.swift:
var body: some View {
List{
ForEach(products, id: \.self) { product in
HStack{
Text(product)
Spacer()
Text(price[products.firstIndex(of: product)])
}
}
}
So my plan here is, to fill each cell with the product name by looping through the product array. Then I want to display the corresponding price. For that I take my second Text and fill it with the price. To find the correct Index of the price array, I get the Index from my products array, since they are similar.
That was my solution, but now I am getting an Error for the products.firstIndex(of: product).
I am getting following Error:
Value of optional type 'Array.Index?' (aka 'Optional') must be unwrapped to a value of type 'Array.Index' (aka 'Int')
I am not really understanding what this Error is trying to tell me.
Can anybody help?
The correct code looks like this:
struct ContentView: View {
private var products = ["Coke", "Fanta", "Sprite", "Water"]
private var prices = ["1,43$", "1,22$", "1,64$", "0,45$"]
var body: some View {
List{
ForEach(products, id: \.self) { product in
HStack{
Text(product)
Spacer()
Text(self.prices[self.products.firstIndex(of: product)!])
}
}
}
}
}
The reason for your error is that the array could be empty at some point which leads to a crash if you try to read firstIndex in that moment.
Now you have two options:
Force-unwrap with an "!" as I did above if you are sure the array will never be cleared/ be empty.
Provide a default value with "?? value" if the value may be nil at some point.

How to transfer new data to array using swift?

I have a tableView(purchases cart), which include magazines in multiple sections. In section, we can see cells with stuff from magazine. In the cell, I did display UILabel(price), UILabel(count) and UILabel with summary price (item * count) in one cell. Also, we can see two buttons (plus and minus), for changing count of item. Example -
var count: Float = Float(cell.countLabel.text!)!
guard Int(count) > 1 else { return }
var shortPrice = cell.newPrice.text
shortPrice?.removeLast(2)
let floatPrice = Float(shortPrice!)
count -= 1
let newSumShortPrice = floatPrice! * count
cell.countLabel.text = String(Int(count))
cell.summPrice.text = "\(newSumShortPrice) ₽"
But changes didn't work with an array.
The strcuct of my model -
struct ViewModel {
var name: String?
var offers: [Offers]?
}
struct Offers : Mappable {
var count : Int?
var fullPrice : String?
var shortPrice : String?
}
var purchasesViewModel = [PurchaseList.Fetch.ViewModel]()
I know, that I must pass changed data (count) to my array and use method tableView.reloadData(). But I can't, because I don't know how to do that.
How I can transfer new count value (check struct Offers) to array purchasesViewModel?
You can go to the index of the array and delete the particular data(old data) from purchasesViewModel and recreate new data and insert it on that index. I hope this will work.

Swift: Merge custom object elements of an Array if they have the same key

I have a Realm Object class called Performance which looks like this
class Performance: Object {
#objc dynamic var move = ""
#objc dynamic var score = 0
}
and when I print the results of the Realm object I get something like this
[Performance {
move = Run;
score = 3;
}, Performance {
move = Walk;
score = 3;
}, Performance {
move = Run;
score = 2;
}]
Then I try to convert the realm results into an Array so that I can merge the performances with the same move and add their score so that I can show the user which of their move has the highest and lowest scores.
I would want to know if there is any chance where I can merge the Performance with the same move and add their score so that the array would look like this.
[Performance {
move = Run;
score = 5;
}, Performance {
move = Walk;
score = 3;
}]
I have tried few solutions like One, two and three.
But they are not really helpful for my problem.
You could iterate through the array and create a dictionary with your move type being the key.
For example:
var totals: [MoveType: Int] = [:]
for item in performances {
totals[item.move] = item.score + (totals[item.move] ?? 0)
}
For each item, this will add the score to the dictionary for the relevant move type, creating a new dictionary entry if it's the first item with that move type.

Sorting a dictionary by key and converting it into an array

I have a dictionary of prices and quantities. I am getting updates on the price and values multiple times in a second so I don't want to store them in an array because dictionary are much faster.
let mainPriceValDict = [Double:Double]()
The data is coming in as an array of JSON so I am using codable to parse the JSON and put it into a dictionary. When I use the data, it needs to be sorted in ascending and/or descending order because I am looping through each price in order to get to a certain total quantity. The format that the array is in that I am looping through is as follows:
let loopingArray = [PriceQuantityEntry]()
struct PriceQuantityEntry {
let price : Double
let size : Double
}
I want to sort the prices which are the keys in the dictionary above and convert them into an array of PriceQuantityEntry. What is the best way to do this? In ascending and deciding order. I have tried first getting all the keys sorted and then grabbing associated values and putting them into the array in order but this seems like more processing than this task actually requires.
I think the best way to do this would be to put a custom initializer in the struct to convert the dictionary value to a value of type PriceQuantityEntry but I am not exactly sure how that would work with the sorting.
This is what I am currently doing to get it to work. I just feel like there is a more efficient way for it to be done. If you feel like I should keep the structure as an array instead of converting it to a dict, let me know.
loopingArray = self.mainPriceValDict.sorted { $0.0 < $1.0 }.map { PriceQuantityEntry(price: $0.0, size: $0.1) }
If you are getting a lot of updates to individual entries, both a dictionary and an array may cause memory copies of the whole memory structure every time an entry is changed.
I would suggest using objects (classes) instead of structures. This will allow you to use both an array and a dictionary to reference the object instances. The dictionary will provide direct access for updates and the array will allow sequential processing in forward or backward order.
[EDIT] Example:
class PriceQuantityEntry
{
static var all:[PriceQuantityEntry] = []
static var prices:[Double:PriceQuantityEntry] = [:]
var price : Double
var size : Double
init(price:Double, size:Double)
{
self.price = price
self.size = size
PriceQuantityEntry.all.append(self)
// PriceQuantityEntry.all.resort() // on demand or when new prices added
PriceQuantityEntry.prices[price] = self
}
class func update(price:Double, with size:Double)
{
if let instance = PriceQuantityEntry.prices[price]
{ instance.size = size }
else
{
let _ = PriceQuantityEntry(price:price, size:size)
PriceQuantityEntry.resort()
}
}
class func resort()
{
PriceQuantityEntry.all.sort{$0.price < $1.price}
}
}
// if adding multiple initial entries before updates ...
let _ = PriceQuantityEntry(price:1, size:3)
let _ = PriceQuantityEntry(price:1.25, size:2)
let _ = PriceQuantityEntry(price:0.95, size:1)
PriceQuantityEntry.resort()
// for updates ...
PriceQuantityEntry.update(price:1, with: 2)
// going throug list ...
var count:Double = 0
var total:Double = 0
var quantity:Double = 5
for entry in PriceQuantityEntry.all
{
total += min(entry.size,quantity-count) * entry.price
count = min(quantity,count + entry.size)
if count == quantity {break}
}

Resources