Swift fixed sized array changes size after every append - arrays

I have created a fixed size array to collect user data from textfields. Each textfield is in a different cell. So I'm collecting all user answers in one fixed array
var userSelectionText = [String](repeating: "", count: 17)
What is really strange is that the array size gets enlarged with +1 every time I insert a string in a certain index in the array according to the row number of the cell.
self.userSelectionText.insert(diseaseSelectionArray[row], at: tappedTextFieldTag)
When I print out the array at first load of the view I have 17 items
["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
After adding a string in a certain index of the fixed sized array I have 17 items + 1
["", "", "No", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
Why? I need them fixed to reflect the exact number of rows in the tableview

You're inserting (adding) elements into the array, but what you want is to replace the existing elements:
var array = ["", "", ""]
print(array)
print(array.count)
array.replaceSubrange(Range(0 ... 0), with: ["1"])
print(array)
print(array.count)
This outputs:
["", "", ""]
3
["1", "", ""]
3
Or, even shorter (thanks #Joakim Danielson):
array[0] = "1"

Related

Picking a random element from Swift Class

This is a model of my makeshift database
var categories: [Category] = [
Category(name: "X",
sign: [Sign(code: "X-1", name: "***", description: "", picture: ""),
Sign(code: "X-2", name: "***", description: "", picture: "")]),
Category(name: "Y",
sign: [Sign(code: "Y-1", name: "Yyy", description: "", picture: ""),
Sign(code: "Y-2", name: "yyy", description: "", picture: "")]),
Category(name: "Z",
sign: [Sign(code: "Z-1", name: "Zzz", description: "", picture: ""),
Sign(code: "Z-2", name: "ZZZ", description: "", picture: "")])
]
I need to get one random element of any category (not one from each) from this base and print it as sign.code
I prepared the variable to store the value:
var RandomSign: Sign
And tried to do it with for loop:
func randomSign() {
for categorie in categories {
randomSign = categorie.sign.randomElement()
But, in the result, my loop generate me random element from each category, and finally save only random from the last one. I would like print for example "X-2" in my consol as random selected value.
Why not pick a random category and then a random sign from that random category?
func randomSign() {
randomSign = categories.randomElement()!.sign.randomElement()!
}
This code will crash if any of the arrays are empty. It would be safer if randomSign was optional then you can do:
func randomSign() {
randomSign = categories.randomElement()?.sign.randomElement()
}
Or you could fallback to a default Sign:
func randomSign() {
randomSign = categories.randomElement()?.sign.randomElement() ?? Sign(code: "empty", name: "***", description: "", picture: "")
}

Nested replace of strings with double quotes in Javascript - Part 2

In Logic Apps I have this function which replaces double double quotes with a zero for some strings.
How can I replicate this in Javascript?
```javascript
replace(replace(replace(replace
(replace(body('HTTP_2'),'"PR_RENT":""','"PR_RENT":0'),
'"PR_ID":""','"PR_ID":0'),'"PR_USUM":""','"PR_USUM":0'),'"PR_LEAS":""','"PR_LEAS":0'),
'"PR_USER8":""','"PR_USER8":0')
```
This question has been answered [here] (Nested replace of strings with double quotes in Javascript). The problem is that this solution required an Integration Account which costs quite a bit of money. Is there any way to do this without an Integration Account?
Besides the solution of javascript with integration account, we can also add azure function in logic app and write the js code in azure function to meet your requirements. Please refer to the steps below:
Create an azure function app in azure portal by referring this tutorial. But this tutorial is creating .net function, we need to create nodejs function, so you should select "Node.js" as "Runtime stack".
In "Hosting" tab, you can choose "Consumption" as "Plan type".
Consumption plan can help us save money, you can refer to this tutorial to know the cost of azure function.
After creating the azure function app, please go to your function app and create a function. Refer to the screenshot below and choose "HTTP trigger" in the next step.
Put the js code to HttpTrigger function.
Then we come back to logic app, create "Azure Functions" connector by referring this tutorial, choose the httptrigger function which you created just now.
Provide your json data as the request body to azure function action.(I have stored your json data as a variable named "data").
Run the logic app, get the result which we expect.
The whole output json data show as below:
{
"Payload": [
{
"RLS_GROUP": "",
"PR_SNAM": "700063",
"PR_OWN": "qqq",
"PR_REF": "",
"PR_NAME": "Bqqq12",
"PR_ADD1": "qqq",
"PR_ADD2": "INDUSTRIAL ESTATE",
"PR_ADD3": "23 INDUSTRIAL ESTATE",
"PR_ADD4": "yyy",
"PR_ADD5": "",
"PR_ADD6": "GB",
"PR_POST": "WQDQWD",
"PR_TEL": "23213",
"PR_TELX": "21312312",
"PR_FAX": "",
"PR_CONT": "",
"PR_NUNIT": "",
"PR_INT": "",
"PR_TENR": "LEASED",
"PR_QDAY": "",
"PR_CLSS": "",
"PR_DRCT": "Closing",
"PR_AGENT": "",
"PR_NOWN": "",
"PR_BOWN": "",
"PR_SOL": "",
"PR_HSTT": "",
"PR_HEND": "",
"PR_HAMT": "",
"PR_PFREQ": "",
"PR_NTENT": "",
"PR_NFLR": "",
"PR_GRA": "",
"PR_WATER": "",
"PR_RATVAL": "",
"PR_RTCT": "",
"PR_SCHG": "",
"PR_OCHG": "",
"PR_GFA": "",
"PR_ZONEA": "",
"PR_ZONEB": "",
"PR_ZONEC": "",
"PR_UPDATE": "",
"PR_UTIME": "",
"PR_UUSER": "",
"PR_HIST": "",
"PR_TAXYN": "",
"PR_TAX": "",
"PR_START": "",
"PR_END": "",
"PR_FREQ": "",
"PR_QTR": "",
"PR_NDUE": "",
"PR_TAXRUN": "",
"PR_OUTLET": "",
"PR_INLET": "",
"PR_VAL": "",
"PR_CST": "",
"PR_FRWA": "",
"PR_FRWB": "",
"PR_PRINT": "",
"PR_NL": "",
"PR_CURRS": "",
"PR_NEXTS": "",
"PR_VAT": "D",
"PR_USER": "",
"PR_VQDAY": "",
"PR_OBS1": "STANDARD NORTH",
"PR_TYPE": "Property",
"PR_VATDATE": "",
"PR_FUTHER": "",
"PR_RESTEN": "",
"PR_CAPGOODS": "",
"PR_INSEE": "",
"PR_CURR": "",
"PR_AQDATE": "20190917",
"PR_USER1": "Office",
"PR_USER2": "Yes",
"PR_USER3": "",
"PR_USER4": "",
"PR_USER5": "20190917",
"PR_USER6": "",
"PR_USER7": "",
"PR_USER8": 0,
"PR_USER9": "",
"PR_USER10": "",
"PR_OBS2": "",
"PR_OBS3": "",
"PR_OBS4": "",
"PR_OBS5": "",
"PR_OBS6": "",
"PR_OBS7": "UK",
"PR_OBS8": "",
"PR_OBS9": "",
"PR_SOLD": "0",
"PR_DATESOLD": "20200917",
"PR_LAND": "",
"PR_LANDUM": "",
"PR_FREE": "",
"PR_ID": 0,
"PR_BTYP": "F",
"PR_LEAS": 0,
"PR_RENT": 0,
"PR_USUM": 0,
"PR_FBUI": 0,
"PR_DREN": "",
"PR_USRC": "",
"PR_RSRC": "",
"PR_LSRC": "",
"PR_ELSR": "",
"PR_EGRS": "",
"PR_PROR": "",
"PR_BSTA": "",
"PR_LNAM": "123123213",
"PR_SITE": "",
"PR_REGION": "",
"PR_DESC": ""
}
]
}
The five fields have been replaced with 0.
The advantage of this solution is the consumption plan cost money only when your functions are running. I think it will be cheaper than integration account.

getting array values ruby

I have an array which is result of a query.
r = [
#<Reservation id: 27, schedule_id: 1, name: "test user", gender: "no_answer",
reservation_num: 1, zip: "", prefecture: "", address: "", street: "",
tel: "", note: "", satisfied: true, canceled: false, seq: 27,
created_at: "2019-08-28 06:04:30", updated_at: "2019-08-28 06:04:30",
created_by: 2, updated_by: 2, from_partner: false, no_counting: false,
reservation_time: nil, one_day_payment: nil, payment_id: 123456>
]
I want to get the payment_id but don't understand how should I write.
If you have number of records in array, you can get payment_id in form of array as below,
r.map(&:payment_id)
Looks like you should be able to select the first reservation from that array and then call on the payment_id. Like:
r.first.payment_id
To get all payment_id's you can do
r.map(&:payment_id)
You can also use the collect method
r.collect(&:name)
You can pluck payment ids (from the required set of data) with the following :
Reservation.pluck(:payment_id)

Reading from a text file in swift and dealing with white space

I'm learning Swift and I'd just like to know the best way to go about reading in a text file, breaking it up into lines and then taking each word on each line and turn the words into strings that can be loaded into class initialisers.
For example, if I have this text file:
**This is just a random text file, and the text on this line
and this line is not needed**
birth year birth month birth day favourite colour
1990 11 12 red
1995 2 4 pink
1992 5 3 orange
1987 3 19 blue
I want to take the birth year, birth month, birth day and favourite colour from each line and then load it into a class like this:
Person(birthYear: 1990, birthMonth: 11, birthDay: 12, favouriteColour: red)
The text file that I want to read in might have an uneven amount of spaces so the output will look like this (for the given text file):
["**This", "is", "just", "a", "random", "text", "file,", "and", "the", "text", "on", "this", "line"]
["and", "this", "line", "is", "not", "needed**"]
["birth", "year", "", "", "", "birth", "month", "", "", "birth", "day", "", "", "", "", "favourite", "colour"]
["1990", "", "", "", "", "", "", "", "", "", "11", "", "", "", "", "", "", "", "", "", "", "", "12", "", "", "", "", "", "", "", "", "", "", "", "red"]
["1995", "", "", "", "", "", "", "", "", "", "", "2", "", "", "", "", "", "", "", "", "", "", "", "", "4", "", "", "", "", "", "", "", "", "", "", "", "pink", ""]
["1992", "", "", "", "", "", "", "", "", "", "", "5", "", "", "", "", "", "", "", "", "", "", "", "", "3", "", "", "", "", "", "", "", "", "", "", "", "orange"]
["1987", "", "", "", "", "", "", "", "", "", "", "3", "", "", "", "", "", "", "", "", "", "", "", "19", "", "", "", "", "", "", "", "", "", "", "", "blue"]
Here is my code so far:
let path = "path to my file"
if let contents = try? String(contentsOfFile: path) {
// breaking the text file up into lines
let lines = contents.components(separatedBy: "\n")
// breaking the lines up into wordsw
for line in lines {
let elements = line.components(separatedBy: " ")
print(elements)
}
}
I'm just wondering what the best way to deal with white space in these cases would be. Thank you in advance for your replies.
You can use a simple solution to clean all Tabs and Double spaces. Try use this piece of code.
func cleanTabsAndSpace(in text:String) -> String {
var newText = text
newText = newText.filter{ $0 != "\t" }.reduce(""){ str, char in
if let lastChar = str.last, lastChar == " " && lastChar == char {
return str
}
return str + String(char)
}
return newText.trimmingCharacters(in: .whitespacesAndNewlines)
}
After create this function you can call it inside your function
if let contents = try? String(contentsOfFile: path) {
// Clean undesired chars
let cleanContent = cleanTabsAndSpace(in: contents)
// breaking the text file up into lines
let lines = cleanContent.components(separatedBy: "\n")
// breaking the lines up into wordsw
for line in lines {
let elements = line.components(separatedBy: " ")
print(elements)
}
}
With this you will have all your content separated as you wish. Now you have just to follow as you want, parsing the content as you want and create your objects.
I'm just considering this structure you described in your question.
Good luck friend and Feel free to contact me if you need something more.
There is a split(separator:maxSplits:omittingEmptySubsequences:)
method which allows to split a String
into an array of SubStrings.
In contrast to components(separatedBy:) this method (by default)
omits empty substrings caused by consecutive separator characters.
Example:
let line = "1990 11 12 red"
let elements = line.split(separator: " ")
print(elements) // ["1990", "11", "12", "red"]
Here element has the type [SubString], i.e. the substrings reference
the original character storage in line, without duplicating it.
If you need "real" strings, then change it to
let elements = line.split(separator: " ").map(String.init)
Applied to your case:
if let contents = try? String(contentsOfFile: path) {
let lines = contents.components(separatedBy: "\n")
for line in lines {
let elements = line.split(separator: " ")
print(elements)
}
}

Array sorting by property

I have a set of object array like this:
[ProductDetails(name: "Tai Tou Choy",
productDescription: "",
deliveryTime: "24 hours",
img1: "https://api.com.my/images/data/4983-img1-1463122485.jpg",
thumbImg1: "https://api.com.my/images/data/thumbs/4983-img1-1463122485.jpg",
img2: "",
thumbImg2: "",
img3: "",
thumbImg3: "",
videoLink: "",
sku: "0000000004983",
relatedProduct: "",
priceOption: [PriceOption(id: 9931,
label: "500g",
price: "4.56",
promo: "0",
quantity: 999)],
deliveryZone: [DeliveryZone(zone: "3")],
deliveryZoneName: [DeliveryZoneName(zoneName: "Within Klang Valley")],
qrCode: "4983"),
ProductDetails(name: "Penguin Asam Jawa",
productDescription: "",
deliveryTime: "24 hours",
img1: "https://api.com.my/images/data/1004-img1.jpg",
thumbImg1: "https://api.com.my/images/data/thumbs/1004-img1.jpg",
img2: "",
thumbImg2: "",
img3: "",
thumbImg3: "",
videoLink: "",
sku: "0000000001004",
relatedProduct: "",
priceOption: [PriceOption(id: 6971,
label: "1 kg",
price: "4.80",
promo: "0",
quantity: 864)],
deliveryZone: [DeliveryZone(zone: "3")],
deliveryZoneName: [DeliveryZoneName(zoneName: "Within Klang Valley")],
qrCode: "1004")]
I wanted to do price sorting from above the arrays, which the PriceOption array is store inside the main array as an object array too. How do i do sorting from PriceOption? I try to do it in this way :
for i in self.productList{
i.priceOption.sortInPlace{
$0.price.localizedCaseInsensitiveCompare($1.price) == NSComparisonResult.OrderedDescending
}
}
Which return an error "Cannot use mutating member on immutable value :'i' is a 'let' constant."
How to do this?
This how I solve issue that you present. I am using sorted method of the array class.
Solution:
self.productList.sorted{ Float($0.priceOption.first!.price)! > Float($1.priceOption.first!.price)! }
Try this
for i in self.productList {
i.priceOption.sort { $0.price < $1.price } }
The answer for swift 3 is as following
Ascending:
self.productList = self.productList.sorted(by:
{(first: ProductDetails, second: ProductDetails) -> Bool in
first.price > second.price
}
)
Descending:
self.productList = self.productList.sorted(by:
{(first: ProductDetails, second: ProductDetails) -> Bool in
first.price > second.price
}
)

Resources