ios - 使用 Int,结合 String 打印现有的 String

标签 ios swift

这里是新手程序员,不明白为什么这样做......

let a1 = "This is some text"
let x : Int = 1

var stringValue = "a\(x)"

print(stringValue)

我希望它打印“这是一些文本”,但它只打印a1

最佳答案

与其将“This is some text”存储在 a1 字符串中,不如将其保存在字典中,以便您可以创建 key (x1、x2 等)来访问它:

let stringDictionary = ["a1": "This is some text", "a2": "This is more text"]

然后您可以使用以下方法将其拉出:

let x : Int = 1
let stringKey = "a\(x)"
let stringValue = stringDictionary[stringKey]
print(stringValue)

关于ios - 使用 Int,结合 String 打印现有的 String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36195967/

相关文章:

ios - 加载 HTML 字符串时 UIWebView 保持空白

c++ - ios opencv 的回调 "processImage"分辨率与 ImageView 不匹配

ios - CFRunLoopPerformBlock 与 dispatch_async

ios - didBeginContact 被同一个 SKPhysicsBody 多次调用

ios - 将 CoreData 保存到 iCloud (Swift)

ios - ObjectiveDDP 和 meteor 更新/插入集合

swift - Linux swift 项目中的 PromiseKit 未按预期工作

swift - Swift 强制转换的运行时成本是多少?

Swift 3 - 有什么方法可以强制调用对象的方法吗?

swift - 如何在 swift 中使用 removeObjectsInArray