Swift-How to write a variable or a value that is changing between parenthesis
53 观看
1回复
42 作者的声誉
(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 的来源 发布者: 2017 年 12 月 27 日回应 1
0像
4287 作者的声誉
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
发布者: 2017 年 12 月 27 日
来自类别的问题 :
- swift 如何以编程方式确定我的应用程序是否在iphone模拟器中运行?
- swift iOS:将UTC NSDate转换为本地时区
- swift 如何在运行时为UIBarButtonItem设置目标和操作
- swift 单个UILabel中的粗体和非粗体文字?
- swift 找到三次贝塞尔曲线上的点的切线
- swift 如何计算两个日期之间的差异?
- swift 如何从UILabel的第n行获取text / String?
- swift 我可以以编程方式滚动到UIPickerView中的所需行吗?
- swift 在UIScrollView中关闭键盘
- swift 如何控制UILabel中的行间距
- swift 用子弹点格式化UILabel?
- swift 如何在iOS中创建本地通知?
- swift UILabel带有两种不同颜色的文本
- swift “界面”构建器中的iOS多行标签
- swift 隐藏UITextField密码
- swift 从NSUserDefaults字典iOS中删除所有键
- swift 将CGContext复制到另一个CGContext中
- swift 如何在Objective-C和Swift中等待
- swift UITextField文本更改事件
- swift 获取系统卷iOS