在 Swift 3 中获取 SKSpriteNode 的文件名 [英] Getting the filename of an SKSpriteNode in Swift 3
本文介绍了在 Swift 3 中获取 SKSpriteNode 的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从 SKSpriteNode
访问图像文件名?我一直在尝试谷歌这个,但似乎没有答案.这是我的代码中无效的部分:
How can I access the image filename from an SKSpriteNode
? I have been trying to Google this, but no answer seems to be available. Here is the impotent part of my code:
current_ingredient?.name = String(describing: current_ingredient?.texture)
print("Filename: \(current_ingredient?.name)")
打印命令返回:
Filename: Optional("Optional(<SKTexture> \'ingredient14\' (110 x 148))")
那么,问题是,我如何只获得ingredient14"?
So, the question is, how do I get only "ingredient14"?
推荐答案
它存储在描述中,所以这里有一个很好的小扩展,我把它撕掉了.
It is stored in the description, so here is a nice little extension I made to rip it out.
extension SKTexture
{
var name : String
{
return self.description.slice(start: "'",to: "'")!
}
}
extension String {
func slice(start: String, to: String) -> String?
{
return (range(of: start)?.upperBound).flatMap
{
sInd in
(range(of: to, range: sInd..<endIndex)?.lowerBound).map
{
eInd in
substring(with:sInd..<eInd)
}
}
}
}
usage:
print(sprite.texture!.name)
这篇关于在 Swift 3 中获取 SKSpriteNode 的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文