1

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.

1
  • 1
    final let is a contradiction in terms. Commented Oct 31, 2017 at 8:00

6 Answers 6

2

You can create a string using string formatting.

String(format:"https://ozsqiqjf.preview.infomaniak.website/empdata_%d.json", variable)
Sign up to request clarification or add additional context in comments.

Comments

2
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)

Comments

2

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"

Comments

2

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"

Comments

1

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.

Comments

-1
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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.