I have the following issue in swift
I have a String
let GET_LISTING = "Listing/GetDetails?deviceid=%#&listingid=%d";
the I use this line to get it formatted
let url = SERVER_URL + String.localizedStringWithFormat(GET_LISTING, deviceId, listingId);
when the number < 1000 it works fine
for example
Listing/GetDetails?deviceid=AB11F1D0-153E-48C3-950F-CC773BBCC683&listingid=500
if number > 1000 it is wrong
Listing/GetDetails?deviceid=AB11F1D0-153E-48C3-950F-CC773BBCC683&listingid=1,050
how can I solve this problem ?
You could convert the listingId to a String:
let GET_LISTING = "Listing/GetDetails?deviceid=%#&listingid=%#"
let url = SERVER_URL + String.localizedStringWithFormat(GET_LISTING, deviceId, String(listingId))
Related
I am developing an application which is in swift and the backend is Odoo. Odoo apis calling is working fine using Alamofire. I have one issue with lead creation api. There is a format for product_id which is: [(6,0,[123,234])]. Here this format is not working if I pass it as a String and it is not accepted without as a string in JSON.
So can anyone tell me which type of format it is and how to pass it in post data?
Here is my code:
with Disctionary
let ids = selectedProducts.value(forKey: kId) as! NSArray
let productIds = NSMutableArray(capacity: 0)
productIds.add("[(6,0")
productIds.add("["+ids.componentsJoined(by: ",")+"])]")
saveDic[kProductId] = productIds.componentsJoined(by: ",")
with string:
let ids = selectedProducts.value(forKey: kId) as! NSArray
let productIds = NSMutableArray(capacity: 0)
productIds.add("[(6,0")
productIds.add("(6,0,["+ids.componentsJoined(by: ",")+"])")
let pid = "(6,0,["+ids.componentsJoined(by: ",")+"])"
postData += ", \"" + kProductId + "\" : [" + pid + "]"
Thank you
What is the best way to split this String into two using Swift?
"https://apod.nasa.gov/apod/http://nusoft.fnal.gov/nova/public/img/FD-evt-echo.gif"
I only need the second part of url in the String. In this example, in this case I just need:
"http://nusoft.fnal.gov/nova/public/img/FD-evt-echo.gif"
let str = "https://apod.nasa.gov/apod/http://nusoft.fnal.gov/nova/public/img/FD-evt-echo.gif"
if let lastStr = str.components(separatedBy: "http").last
{
let result = "http" + lastStr
print(result)
}
Console Output: http://nusoft.fnal.gov/nova/public/img/FD-evt-echo.gif
A question I think pretty simple but I never had to do it in swift. it's pretty simple PHP but here I do not find my solution on the internet.
ask: I would like to add a variable in this chain of character. Instead of 123, I would need a variable.
final let urlString = "https://ozsqiqjf.preview.infomaniak.website/empdata_123.json"
result = final let urlString = "https://ozsqiqjf.preview.infomaniak.website/empdata_VARAIBLE.json"
Can you give me the syntax in swift3 or direct me to a good tutorial.
You can create a string using string formatting.
String(format:"https://ozsqiqjf.preview.infomaniak.website/empdata_%d.json", variable)
let variable = 123
final let urlString = "https://ozsqiqjf.preview.infomaniak.website/empdata_\(variable).json"
\(variable) is what you need
OR
use string formatting
let variable = 123
final let urlString = String(format:"https://ozsqiqjf.preview.infomaniak.website/empdata_%d.json", variable)
There is good documentation about Strings in Swift Language Guide. Your options are:
Concatenating Strings
let urlString = "https://ozsqiqjf.preview.infomaniak.website/empdata_" + value + ".json"
String interpolation
let urlString = "https://ozsqiqjf.preview.infomaniak.website/empdata_\(value).json"
Swift4 You can add a string in these ways:
var myString = "123" // Or VARAIBLE Here any string you pass!!
var urlString = "https://ozsqiqjf.preview.infomaniak.website/empdata_\(myString).json"
A simple way of doing it could be:
final let urlString = "https://ozsqiqjf.preview.infomaniak.website/empdata_" + variablename + ".json"
You can also do it like this (a little more typesafe):
final let urlString = "https://ozsqiqjf.preview.infomaniak.website/empdata_\(variablename).json"
Swift will read \(variablename) into the string automatically and accepts - among all things - integers.
let variable = 123
final let urlString = "https://ozsqiqjf.preview.infomaniak.website/empdata_" + variable + ".json"
or
final let urlString = "https://ozsqiqjf.preview.infomaniak.website/empdata_\(variable).json"
I have the following problem with iOS calculations when the user is in different locales. Here in Europe the decimal point separator is a "," and in other locales it might be a ".". As long as the user is in a locale with a "." all calculations work fine, but when a comma is used instead I get an error. Here's the simple code so far:
if let expense = expenseTextField.text
let expenseValue = Double(expense)
let guests = guestTextField.text
let guestValue = Double(guests) {
let result = round((expenseValue / guestValue) * 100) / 100
resultLabel.text = priceString
//resultLabel.text = String(result)
} else {
resultLabel.text = "No values!"
}
I am pretty sure I have to use the numberFormatter as far as I understand other questions on here, but I don't get how to do this and the documentation doesn't provide a sample how to create a double or float from a string. I tried the following but I get an error stating that I cannot neither create doubles nor use these variables for calculations:
if let expense = expenseTextField.text
let expenseValue = String(expense),
let guests = guestTextField.text
let guestValue = String(guests) {
let expenseFormatter = NumberFormatter()
expenseFormatter.number(from: expenseValue)
let guestFormatter = NumberFormatter()
guestFormatter.number(from: guestValue)
let expenseDouble = Double(expenseFormatter)
let guestDouble = Double(guestFormatter)
let result = round((expenseDouble / guestDouble) * 100) / 100
resultLabel.text = priceString
} else {
resultLabel.text = "No values!"
}
I am not sure how to create real Doubles from those strings with numberFormatter and then use them in calculations.
Can someone give me a tip?
Thanks
The only thing you need is to write the method call correctly:
let expenseDouble = expenseFormatter.number(from: expenseValue)?.doubleValue ?? 0
let guestDouble = guestFormatter.number(from: guestValue)?.doubleValue ?? 0
A formatter/parser directly converts string to number. You then have to take the Double value from it. Note that parsing can fail and you have to solve the resulting optional in that case, e.g. using ?? 0.
Is there any way to get URL and further its parameter values in f# Only F# any one can help me. I have tried a lot but no solution found
http://www.example.com/blog?blogid=23
Want to get http://www.example.com/blog?blogid=23
Then 23
let getBlogId uriString =
let uri = System.Uri(uriString)
let kvps = HttpUtility.ParseQueryString uri.Query
let idStr = kvps.["blogid"]
int idStr
let uri = new System.Uri("http://www.example.com/blog?blogid=23")
let blogId = uri.Query.Split('=').[1]