请帮助我。我更新为Xcode 10
,其中Swift 4.2
。现在,%ld
格式(还有其他格式)不再起作用。谁知道我该怎么用呢?
let name = String(format: "Pic_%ld", value)
部分代码:
if value != 0 && value != 6 && value != 9 {
let name = String(format: "Pic_%ld", value)
print("Pic_%ld")
print(value)
let tileNode = SKSpriteNode(imageNamed: name)
print(name)
tileNode.size = CGSize(width: Width, height: Height)
var point = pointFor(column: column, row: row)
point.x -= Width/2
point.y -= Height/2
tileNode.position = point
tilesLayer.addChild(tileNode)
}
看看
Pic_%ld
-4
2018-09-23 19:46:52.799361+0800 [20784:2265405] SKTexture: Error loading image resource: "Pic_-4"
Pic_-4
答案 0 :(得分:0)
您可以通过以下示例格式化任何字符串。
string = " \(aNumber) and \(aString)" // wher aNumber is a Int , aString is a string Variable
在这种情况下,应该是:
name = "Pic_\(value)"
答案 1 :(得分:0)
所以您的代码可以正常工作。
// This command prints "Pic_%ld" because there is no formatting, it's just a string
print("Pic_%ld")
// This command prints "-4" because it's the value of the variable
print(value)
// And these commands prints "Pic_-4". Because your value is -4
let name = String(format: "Pic_%ld", value)
print(name)
您也会收到此错误
SKTexture:加载图像资源“ Pic_-4”时出错
由于明显的原因,您的资源中没有这样的图像。但是无论如何,String
格式在您的情况下效果很好。