-3

(in swift language) For example " A + D " I want the string A to stay all the time but the value of D changes depending on let's say Hp, so when Hp is fd the string will be "A + fd" and etc

I mean like( "A + %s" % Hp ) for the string in python. Such as here: What does %s mean in Python?

Mohan
  • 135
  • 1
  • 7
  • There is nothing builtin for that as far as I know, you have to trigger an update to "A + D" every time D changes. – luk2302 Dec 27 '17 at 17:13
  • @luk2302 I mean like %s between parenthesis in python – Mohan Dec 27 '17 at 17:23
  • 1
    Please don't add irrelevant tags to your question and especially don't add them again once they were removed through editing. Your question is clearly about a Swift language feature, so it has nothing to do with xcode, ios or iphone. Tags shouldn't be used to list all technologies you use, they should be used to mark the specific technologies your language concerns. – Dávid Pásztor Dec 27 '17 at 17:23

1 Answers1

1

If you are talking about %s, then it's a c-style formatting key, which awaits string variable or value in the list of arguments. In Swift, you compose strings using "\(variable)" syntax, which is called String interpolation, as explained in the documentation:

String Interpolation

String interpolation is a way to construct a new String value from a mix of constants, variables, literals, and expressions by including their values inside a string literal. You can use string interpolation in both single-line and multiline string literals. Each item that you insert into the string literal is wrapped in a pair of parentheses, prefixed by a backslash ():

Source: official documentation

Example:

var myVar = "World"
var string = "Hello \(myVar)"

With non-strings:

let multiplier = 3 
let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)" 
// Output: message is "3 times 2.5 is 7.5"
Hexfire
  • 5,945
  • 8
  • 32
  • 42